Skip to content

Commit

Permalink
Showing 6 changed files with 86 additions and 9 deletions.
19 changes: 19 additions & 0 deletions spec/compiler/codegen/pointer_spec.cr
Original file line number Diff line number Diff line change
@@ -438,4 +438,23 @@ describe "Code gen: pointer" do
1
))
end

it "generates correct code for Pointer.malloc(0) (#2905)" do
run(%(
require "prelude"
class Foo
def initialize(@value : Int32)
end
def value
@value
end
end
foo = Foo.new(3)
Pointer(Int32 | UInt8[9]).malloc(0_u64)
foo.value
)).to_i.should eq(3)
end
end
1 change: 1 addition & 0 deletions spec/std/http/client/client_spec.cr
Original file line number Diff line number Diff line change
@@ -37,6 +37,7 @@ module HTTP
{% end %}

typeof(Client.post_form "url", {"a" => "b"})
typeof(Client.post_form("url", {"a" => "b"}) { })
typeof(Client.new("host").basic_auth("username", "password"))
typeof(Client.new("host").before_request { |req| })
typeof(Client.new("host").close)
5 changes: 3 additions & 2 deletions src/compiler/crystal/codegen/codegen.cr
Original file line number Diff line number Diff line change
@@ -1886,7 +1886,7 @@ module Crystal
if malloc_fun = @malloc_fun
malloc_fun = check_main_fun MALLOC_NAME, malloc_fun
size = trunc(type.size, LLVM::Int32)
pointer = call malloc_fun, [size]
pointer = call malloc_fun, size
bit_cast pointer, type.pointer
else
builder.malloc type
@@ -1900,7 +1900,8 @@ module Crystal
size = trunc(type.size, LLVM::Int32)
count = trunc(count, LLVM::Int32)
size = builder.mul size, count
pointer = call malloc_fun, [size]
pointer = call malloc_fun, size
memset pointer, int8(0), size
bit_cast pointer, type.pointer
else
builder.array_malloc(type, count)
1 change: 0 additions & 1 deletion src/compiler/crystal/codegen/primitives.cr
Original file line number Diff line number Diff line change
@@ -422,7 +422,6 @@ class Crystal::CodeGenVisitor
type = node.type.as(PointerInstanceType)
llvm_type = llvm_embedded_type(type.element_type)
last = array_malloc(llvm_type, call_args[1])
memset last, int8(0), llvm_type.size
last
end

59 changes: 53 additions & 6 deletions src/http/client.cr
Original file line number Diff line number Diff line change
@@ -358,6 +358,24 @@ class HTTP::Client
exec request
end

# Executes a POST with form data and yields the response to the block.
# The response will have its body as an `IO` accessed via `HTTP::Client::Response#body_io`.
# The "Content-type" header is set to "application/x-www-form-urlencoded".
#
# ```
# client = HTTP::Client.new "www.example.com"
# client.post_form("/", "foo=bar") do |response|
# response.body_io.gets
# end
# ```
def post_form(path, form : String, headers : HTTP::Headers? = nil)
request = new_request("POST", path, headers, form)
request.headers["Content-type"] = "application/x-www-form-urlencoded"
exec(request) do |response|
yield response
end
end

# Executes a POST with form data. The "Content-type" header is set
# to "application/x-www-form-urlencoded".
#
@@ -366,15 +384,27 @@ class HTTP::Client
# response = client.post_form "/", {"foo": "bar"}
# ```
def post_form(path, form : Hash, headers : HTTP::Headers? = nil) : HTTP::Client::Response
body = HTTP::Params.build do |form_builder|
form.each do |key, value|
form_builder.add key, value
end
end

body = HTTP::Params.from_hash(form)
post_form path, body, headers
end

# Executes a POST with form data and yields the response to the block.
# The response will have its body as an `IO` accessed via `HTTP::Client::Response#body_io`.
# The "Content-type" header is set to "application/x-www-form-urlencoded".
#
# ```
# client = HTTP::Client.new "www.example.com"
# client.post_form("/", {"foo": "bar"}) do |response|
# response.body_io.gets
# end
# ```
def post_form(path, form : Hash, headers : HTTP::Headers? = nil)
body = HTTP::Params.from_hash(form)
post_form(path, body, headers) do |response|
yield response
end
end

# Executes a POST with form data. The "Content-type" header is set
# to "application/x-www-form-urlencoded".
#
@@ -387,6 +417,23 @@ class HTTP::Client
end
end

# Executes a POST with form data and yields the response to the block.
# The response will have its body as an `IO` accessed via `HTTP::Client::Response#body_io`.
# The "Content-type" header is set to "application/x-www-form-urlencoded".
#
# ```
# HTTP::Client.post_form("http://www.example.com", "foo=bar") do |response|
# response.body_io.gets
# end
# ```
def self.post_form(url, form : String | Hash, headers : HTTP::Headers? = nil, tls = nil)
exec(url, tls) do |client, path|
client.post_form(path, form, headers) do |response|
yield response
end
end
end

# Executes a request.
# The response will have its body as a `String`, accessed via `HTTP::Client::Response#body`.
#
10 changes: 10 additions & 0 deletions src/http/params.cr
Original file line number Diff line number Diff line change
@@ -64,6 +64,16 @@ module HTTP
end
end

# Creates an HTTP::Params instance from the key-value
# pairs of the given *hash*.
def self.from_hash(hash : Hash)
build do |builder|
hash.each do |key, value|
builder.add key, value
end
end
end

# Builds an url-encoded HTTP form/query.
#
# The yielded object has an `add` method that accepts two arguments,

0 comments on commit 1fd3ce7

Please sign in to comment.