File: image-with-headers-stash.py

package info (click to toggle)
firefox 143.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,617,328 kB
  • sloc: cpp: 7,478,492; javascript: 6,417,157; ansic: 3,720,058; python: 1,396,372; xml: 627,523; asm: 438,677; java: 186,156; sh: 63,477; makefile: 19,171; objc: 13,059; perl: 12,983; yacc: 4,583; cs: 3,846; pascal: 3,405; lex: 1,720; ruby: 1,003; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 53; csh: 10
file content (45 lines) | stat: -rw-r--r-- 1,438 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# Supports two operations:
# - ?image=uuid: Returns an image, and records the request headers that were
#   used to get that image.
# - ?read=uuid: Returns the request headers in the stash keyed by a given uuid.

import os
import json

from wptserve.utils import isomorphic_decode

def main(request, response):
  if b"image" in request.GET:
    uuid = request.GET[b"image"]

    header_pairs = []
    for header_name in request.headers.keys():
        # ensure the header name/value are unicode strings
        name = header_name.lower().decode("utf-8")
        for header_value in request.headers.get_list(header_name):
            value = header_value.decode("utf-8")
            header_pairs.append([name, value])

    request_headers = json.dumps(header_pairs)
    request.server.stash.put(uuid, request_headers)

    # Return a basic image.
    response_headers = [
      (b"Content-Type", b"image/png"),
      (b"Access-Control-Allow-Origin", b"*")
    ]
    image_path = os.path.join(
      os.path.dirname(isomorphic_decode(__file__)),
      u"../../../common/square.png"
    )
    return (200, response_headers, open(image_path, mode='rb').read())

  elif b"read" in request.GET:
    uuid = request.GET[b"read"]
    stash_value = request.server.stash.take(uuid)

    if stash_value is None:
      stash_value = "null"
    return (200, [(b"Content-Type", b"application/json")], str(stash_value))

  return (404 , [], "Not found")