Skip to content

Commit

Permalink
Return the cookies sent as headers.
Browse files Browse the repository at this point in the history
  • Loading branch information
gunchev committed Jan 25, 2024
1 parent c05dee7 commit 0e5a4f7
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions httpdecho.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
"""A Simple Python HTTP server that echos the request in the response."""

import argparse
from email.generator import BytesGenerator
import email.message
from http.server import BaseHTTPRequestHandler, HTTPServer
import socket
from email.generator import BytesGenerator
from http.cookies import SimpleCookie
from http.server import BaseHTTPRequestHandler, HTTPServer
from urllib import parse

__version__ = '0.3.0'


__all__ = ["EchoHTTPRequestHandler"]


Expand All @@ -25,11 +25,11 @@ def do_GET(self): # noqa:N802, pylint: disable=invalid-name
self.send_head()
BytesGenerator(self.wfile).flatten(message, unixfrom=False)

do_HEAD = do_GET # noqa:N815
do_HEAD = do_GET # noqa:N815

do_OPTIONS = do_GET # noqa:N815

do_DELETE = do_GET # noqa:N815
do_DELETE = do_GET # noqa:N815

def do_POST(self): # noqa:N802, pylint: disable=invalid-name
"""Echo a request with a body."""
Expand All @@ -44,14 +44,15 @@ def do_POST(self): # noqa:N802, pylint: disable=invalid-name
self.send_head()
BytesGenerator(self.wfile).flatten(message, unixfrom=False)

do_PUT = do_POST # noqa:N815
do_PUT = do_POST # noqa:N815

do_PATCH = do_POST # noqa:N815

def send_head(self):
"""Send all the basic, required headers."""
self.send_response(200)
self.send_header("Content-Type", "text/rfc822-headers; charset=UTF-8")
# self.send_header("Content-Type", "text/rfc822-headers; charset=UTF-8")
self.send_header("Content-Type", "text/plain; charset=UTF-8")
self.send_header("Last-Modified", self.date_time_string())
self.end_headers()

Expand All @@ -63,8 +64,12 @@ def get_message(self):

server_url = parse.SplitResult("http", f"{self.server.server_name}:{self.server.server_port}", "", "", "")
request_url = parse.urlsplit(server_url.geturl() + self.path)
for header, value in parse.parse_qs(request_url.query).items():
message.add_header(header, value[0])
for name, value in parse.parse_qs(request_url.query).items():
message.add_header(name, value[0])

cookies = SimpleCookie(self.headers.get('Cookie'))
for name, value in cookies.items():
message.add_header(f'Cookie-{name}', repr(value))

return message

Expand Down

0 comments on commit 0e5a4f7

Please sign in to comment.