|
51 | 51 | parse_multi_value_header, |
52 | 52 | next_stale_after_value, |
53 | 53 | digest_challenge_response, |
| 54 | + normalize_charset, |
54 | 55 | ) |
55 | 56 | from .utils import weighted_choice |
56 | 57 | from .structures import CaseInsensitiveDict |
@@ -1407,20 +1408,96 @@ def cache_control(value): |
1407 | 1408 | return response |
1408 | 1409 |
|
1409 | 1410 |
|
1410 | | -@app.route("/encoding/utf8") |
1411 | | -def encoding(): |
1412 | | - """Returns a UTF-8 encoded body. |
| 1411 | +@app.route("/encoding/<charset>") |
| 1412 | +def encoding(charset): |
| 1413 | + """Returns the requested charset and encoding. |
1413 | 1414 | --- |
1414 | 1415 | tags: |
1415 | 1416 | - Response formats |
| 1417 | + parameters: |
| 1418 | + - in: path |
| 1419 | + name: charset |
| 1420 | + type: |
| 1421 | + default: 'utf8' |
| 1422 | + - in: query |
| 1423 | + name: content-type |
| 1424 | + type: string |
| 1425 | + description: The content type of the response. If unset will use response content type ("accept" header). |
| 1426 | + default: '' |
| 1427 | + produces: |
| 1428 | + - text/html |
| 1429 | + - text/plain |
| 1430 | + - '*/*' |
| 1431 | + responses: |
| 1432 | + 200: |
| 1433 | + description: Content with the requested encoding and content type. |
| 1434 | + """ |
| 1435 | + return encoding_generic(charset, None) |
| 1436 | + |
| 1437 | + |
| 1438 | +@app.route("/encoding/<charset>/<body>") |
| 1439 | +def encoding_generic(charset, body): |
| 1440 | + """Returns the requested charset and encoding. |
| 1441 | + --- |
| 1442 | + tags: |
| 1443 | + - Response formats |
| 1444 | + parameters: |
| 1445 | + - in: path |
| 1446 | + name: charset |
| 1447 | + type: |
| 1448 | + default: 'utf8' |
| 1449 | + - in: query |
| 1450 | + name: content-type |
| 1451 | + type: string |
| 1452 | + description: The content type of the response. If unset will use response content type ("accept" header). |
| 1453 | + default: '' |
| 1454 | + - in: path |
| 1455 | + name: body |
| 1456 | + type: string |
| 1457 | + default: SFRUUEJJTiDjga_mnIDpq5jjgafjgZk= |
1416 | 1458 | produces: |
1417 | 1459 | - text/html |
| 1460 | + - text/plain |
| 1461 | + - '*/*' |
1418 | 1462 | responses: |
1419 | 1463 | 200: |
1420 | | - description: Encoded UTF-8 content. |
| 1464 | + description: Content with the requested encoding and content type and body. |
1421 | 1465 | """ |
| 1466 | + response = make_response() |
| 1467 | + |
| 1468 | + charset = charset or request.headers.get("accept-charset", "utf-8") |
| 1469 | + accept_header = request.headers.get("accept") |
| 1470 | + if accept_header is not None: |
| 1471 | + accept_header = accept_header.split(";")[0].split(",")[0] |
| 1472 | + response.content_type = (request.args.get("content-type", accept_header) or "text/html") + "; charset=" + charset |
| 1473 | + normalized_charset = (normalize_charset(charset) or "utf-8").lower() |
1422 | 1474 |
|
1423 | | - return render_template("UTF-8-demo.txt") |
| 1475 | + if body: |
| 1476 | + response.data = base64.urlsafe_b64decode(body) |
| 1477 | + return response |
| 1478 | + elif normalized_charset in ["utf-8", "utf-16", "utf-32"]: |
| 1479 | + template_data = { |
| 1480 | + "title": "Unicode Demo", |
| 1481 | + "citation_url": "http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-demo.txt", |
| 1482 | + "body_template": "encoding/utf-8.txt", |
| 1483 | + "citation_prefix": ("Taken from" if normalized_charset == "utf-8" |
| 1484 | + else f"Re-encoded to {normalized_charset} from the utf-8 taken from") |
| 1485 | + } |
| 1486 | + else: |
| 1487 | + template_data = { |
| 1488 | + "title": f"{normalized_charset} Demo", |
| 1489 | + "citation_url": "", |
| 1490 | + "body_template": f"encoding/{normalized_charset}.txt", |
| 1491 | + "citation_prefix": "" |
| 1492 | + } |
| 1493 | + |
| 1494 | + if response.content_type.startswith("text/html"): |
| 1495 | + template_name = "encoding/demo.html.j2" |
| 1496 | + else: |
| 1497 | + template_name = template_data["body_template"] |
| 1498 | + response.data = render_template(template_name, **template_data).encode(normalized_charset) |
| 1499 | + |
| 1500 | + return response |
1424 | 1501 |
|
1425 | 1502 |
|
1426 | 1503 | @app.route("/bytes/<int:n>") |
|
0 commit comments