Skip to content

Instantly share code, notes, and snippets.

@netfeed
Created August 24, 2015 16:48
Show Gist options
  • Select an option

  • Save netfeed/78389deddaae8d35313d to your computer and use it in GitHub Desktop.

Select an option

Save netfeed/78389deddaae8d35313d to your computer and use it in GitHub Desktop.
diff --git a/src/http/request.cr b/src/http/request.cr
index 8cc855f..d64e7b9 100644
--- a/src/http/request.cr
+++ b/src/http/request.cr
@@ -7,7 +7,6 @@ class HTTP::Request
getter headers
getter body
getter version
- getter cookies
def initialize(@method : String, @path, @headers = Headers.new : Headers, @body = nil, @version = "HTTP/1.1", @cookies = Cookies.new : Cookies)
if body = @body
@@ -30,14 +29,21 @@ class HTTP::Request
HTTP.serialize_headers_and_body(io, @cookies.add_request_headers(@headers), @body)
end
+ def cookies
+ if headers.get? "Cookie"
+ @cookies = Cookies.from_headers(headers)
+ end
+
+ @cookies
+ end
+
def self.from_io(io)
request_line = io.gets
return unless request_line
method, path, http_version = request_line.split
HTTP.parse_headers_and_body(io) do |headers, body|
- cookies = Cookies.from_headers(headers)
- return new method, path, headers, body.try &.read, http_version, cookies
+ return new method, path, headers, body.try &.read, http_version
end
# Unexpected end of http request
diff --git a/src/http/response.cr b/src/http/response.cr
index 62242a3..91fc25e 100644
--- a/src/http/response.cr
+++ b/src/http/response.cr
@@ -5,7 +5,6 @@ class HTTP::Response
getter status_code
getter status_message
getter headers
- getter cookies
getter! body_io
property upgrade_handler
@@ -50,6 +49,14 @@ class HTTP::Response
HTTP.serialize_headers_and_body(io, @cookies.add_response_headers(@headers), @body)
end
+ def cookies
+ if headers.get? "Set-Cookie"
+ @cookies = Cookies.from_headers(headers)
+ end
+
+ @cookies
+ end
+
def self.from_io(io)
line = io.gets
if line
@@ -58,8 +65,7 @@ class HTTP::Response
status_message = status_message.chomp
HTTP.parse_headers_and_body(io) do |headers, body|
- cookies = Cookies.from_headers(headers)
- return new status_code, body.try &.read, headers, status_message, http_version, cookies
+ return new status_code, body.try &.read, headers, status_message, http_version
end
end
@@ -74,8 +80,7 @@ class HTTP::Response
status_message = status_message.chomp
HTTP.parse_headers_and_body(io) do |headers, body|
- cookies = Cookies.from_headers(headers)
- return yield new status_code, nil, headers, status_message, http_version, cookies, body
+ return yield new status_code, nil, headers, status_message, http_version, Cookies.new, body
end
end
@waterlink
Copy link

Your #cookies method will parse each time you call it, which is bad. Should be something along these lines:

def cookies
  (@cookies ||= parse_cookies).not_nil!
end

private def parse_cookies
  # Your code here to parse cookies.
  # They should never be nil
  # If there is no Cookie header, should be an empty `Cookie` instance
end

@netfeed
Copy link
Author

netfeed commented Sep 6, 2015

No, it shouldn't. The header is removed once it's parsed

@waterlink
Copy link

Hmm, that is quite unexpected behavior if you ask me..

@netfeed
Copy link
Author

netfeed commented Sep 6, 2015

It's to make sure that Set-Cookie/Cookie is synced. What happens if you change one of the cookies so it's different from the header? Should you then traverse all of them and do the update twice instead of just one time? The header isn't needed until you need to write them again if it that's needed then the Cookie class is more than suited for that.

@waterlink
Copy link

I see.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment