Skip to content

Proposed solution for Issue #104: multiple cookies #108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion adafruit_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,10 @@ def _parse_headers(self) -> None:
self._remaining = int(content)
if title == "transfer-encoding":
self._chunked = content.strip().lower() == "chunked"
self._headers[title] = content
if title == "set-cookie" and title in self._headers:
self._headers[title] += ", " + content
else:
self._headers[title] = content

def _validate_not_gzip(self) -> None:
"""gzip encoding is not supported. Raise an exception if found."""
Expand Down
58 changes: 58 additions & 0 deletions examples/requests_multiple_cookies.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# SPDX-FileCopyrightText: 2022 Alec Delaney
# SPDX-License-Identifier: MIT

"""
This example was written for the MagTag; changes may be needed
for connecting to the internet depending on your device.
"""

import ssl
import wifi
import socketpool
import adafruit_requests

COOKIE_TEST_URL = "https://www.adafruit.com"

# Get wifi details and more from a secrets.py file
try:
from secrets import secrets
except ImportError:
print("WiFi secrets are kept in secrets.py, please add them there!")
raise

# Connect to the Wi-Fi network
print("Connecting to %s" % secrets["ssid"])
wifi.radio.connect(secrets["ssid"], secrets["password"])

# Set up the requests library
pool = socketpool.SocketPool(wifi.radio)
requests = adafruit_requests.Session(pool, ssl.create_default_context())

# GET from the URL
print("Fetching multiple cookies from", COOKIE_TEST_URL)
response = requests.get(COOKIE_TEST_URL)

# Spilt up the cookies by ", "
elements = response.headers["set-cookie"].split(", ")

# NOTE: Some cookies use ", " when describing dates. This code will iterate through
# the previously split up 'set-cookie' header value and piece back together cookies
# that were accidentally split up for this reason
days_of_week = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat")
elements_iter = iter(elements)
cookie_list = []
for element in elements_iter:
captured_day = [day for day in days_of_week if element.endswith(day)]
if captured_day:
cookie_list.append(element + ", " + next(elements_iter))
else:
cookie_list.append(element)

# Pring the information about the cookies
print("Number of cookies:", len(cookie_list))
print("")
print("Cookies received:")
print("-" * 40)
for cookie in cookie_list:
print(cookie)
print("-" * 40)