Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
lost committed May 2, 2024
1 parent 210d975 commit f7c552c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 98 deletions.
55 changes: 0 additions & 55 deletions .ameba.yml

This file was deleted.

26 changes: 11 additions & 15 deletions spec/puppy_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,18 @@ describe Puppy do
JSON.parse(resp.body_io)["gzipped"].as_bool.should be_true
end

# Failed both WinHttp and crystal std Compress::Deflate::Reader
#
# trace: https://github.com/crystal-lang/crystal/issues/5221
#
# it "deflate" do
# # curl -X GET "https://httpbin.org/deflate" -H "accept: application/json"
# #
# headers = HTTP::Headers.new
# headers["content-type"] = "application/json"
# url = "https://httpbin.org/deflate"
# method = "GET"
# req = HTTP::Request.new method, url, headers: headers
it "deflate" do
# curl -X GET "https://httpbin.org/deflate" -H "accept: application/json"
#
headers = HTTP::Headers.new
headers["content-type"] = "application/json"
url = "https://httpbin.org/deflate"
method = "GET"
req = HTTP::Request.new method, url, headers: headers

# resp = Puppy.fetch req, 10.seconds
# JSON.parse(resp.body_io)["deflated"].as_bool.should be_true
# end
resp = Puppy.fetch req, 10.seconds
JSON.parse(resp.body_io)["deflated"].as_bool.should be_true
end

it "status code" do
# curl -X GET "https://httpbin.org/status/444" -H "accept: text/plain"
Expand Down
39 changes: 11 additions & 28 deletions src/puppy.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ require "uri"
require "http/headers"
require "http/request"
require "http/client/response"

require "compress/gzip"
require "compress/zlib"

require "./puppy/lib_winhttp"
require "./puppy/version"
Expand All @@ -13,7 +15,7 @@ module Puppy
RESPONSE_BODY_INIT_SIZE = 8 * 1024
UA = "Puppy #{VERSION} by WinHttp"

class Puppy::Error < Exception
class Error < Exception
end

# :nodoc:
Expand Down Expand Up @@ -118,23 +120,6 @@ module Puppy
raise Error.new "WinHttpAddRequestHeaders error: " + LibC.GetLastError.to_s
end

{% if flag?(:without_zlib) %}
if decompress
# set decompression option
#
decompression_flags = LibWinHttp::WINHTTP_DECOMPRESSION_FLAG_ALL
dbg! decompression_flags
if LibWinHttp.WinHttpSetOption(
hRequest,
LibWinHttp::WINHTTP_OPTION_DECOMPRESSION,
pointerof(decompression_flags),
sizeof(typeof(decompression_flags))
) == 0
raise Error.new "Set decompression option: WinHttpSetOption error: " + LibC.GetLastError.to_s
end
end
{% end %}

# send request
#
request_body_buf = req.body.try &.getb_to_end || Bytes.new(0)
Expand Down Expand Up @@ -249,7 +234,7 @@ module Puppy

# convert response headers to HTTP::Headers
#
response_headers = parseResponseHeaders response_headers_buf
response_headers = parse_response_headers response_headers_buf
dbg! response_headers
if response_headers.size == 0
raise Error.new "Failed to parsing response headers"
Expand Down Expand Up @@ -285,13 +270,11 @@ module Puppy

# decompress response body
#
{% if !flag?(:without_zlib) %}
if decompress
content_encoding = response_headers["content-encoding"]?
dbg! content_encoding
response_body_io = decompress(response_body_io, format: content_encoding) if content_encoding
end
{% end %}
if decompress
content_encoding = response_headers["content-encoding"]?
dbg! content_encoding
response_body_io = decompress(response_body_io, format: content_encoding) if content_encoding
end

# return HTTP::Client::Response
#
Expand All @@ -303,7 +286,7 @@ module Puppy
end
end

private def self.parseResponseHeaders(buf : Slice(UInt16)) : HTTP::Headers
private def self.parse_response_headers(buf : Slice(UInt16)) : HTTP::Headers
result = HTTP::Headers.new
String.from_utf16(buf)
.split(CRLF, remove_empty: true)
Expand All @@ -320,7 +303,7 @@ module Puppy
when "gzip"
Compress::Gzip::Reader.new io, sync_close: true
when "deflate"
Compress::Deflate::Reader.new io, sync_close: true
Compress::Zlib::Reader.new io, sync_close: true
else
io
end
Expand Down

0 comments on commit f7c552c

Please sign in to comment.