Skip to content
This repository has been archived by the owner on May 17, 2019. It is now read-only.

Commit

Permalink
Merge pull request #100 from JuliaWeb/iofix
Browse files Browse the repository at this point in the history
Adjustments for IO refactoring
  • Loading branch information
malmaud committed Feb 9, 2016
2 parents 9797063 + 19938c3 commit 572c984
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
3 changes: 2 additions & 1 deletion REQUIRE
Expand Up @@ -2,7 +2,8 @@ julia 0.4
HttpCommon 0.2.4
HttpParser
URIParser 0.1.1
MbedTLS 0.1.4
MbedTLS 0.2.1
Codecs
JSON
Libz
Compat 0.7.9
15 changes: 11 additions & 4 deletions src/Requests.jl
Expand Up @@ -7,10 +7,17 @@ export view, save
export set_proxy, set_https_proxy, get_request_settings

import Base: get, write
import Base.FS: File

if VERSION < v"0.5.0-dev+1229"
import Base.FS: File
else
import Base.Filesystem: File
end

import URIParser: URI
import HttpCommon: Cookie

using Compat
using HttpParser
using HttpCommon
using URIParser
Expand Down Expand Up @@ -81,7 +88,7 @@ for kind in [:Response, :Request]
@eval text(r::$kind) = utf8(bytes(r))
@eval Base.bytestring(r::$kind) = text(r)
@eval Base.readall(r::$kind) = text(r)
@eval Base.readbytes(r::$kind) = bytes(r)
@eval Base.read(r::$kind) = bytes(r)
@eval json(r::$kind; kwargs...) = JSON.parse(text(r); kwargs...)

## Response getters to future-proof against changes to the Response type
Expand Down Expand Up @@ -278,10 +285,10 @@ end
function do_request(uri::URI, verb; kwargs...)
response_stream = do_stream_request(uri, verb; kwargs...)
response = response_stream.response
response.data = readbytes(response_stream)
response.data = read(response_stream)
if get(response.headers, "Content-Encoding", "") ("gzip", "deflate")
if !isempty(response.data)
response.data = response.data |> ZlibInflateInputStream |> readbytes
response.data = response.data |> ZlibInflateInputStream |> read
end
end
response
Expand Down
7 changes: 4 additions & 3 deletions src/streaming.jl
Expand Up @@ -111,18 +111,19 @@ function Base.eof(stream::ResponseStream)
eof(stream.buffer) && (stream.state==BodyDone || eof(stream.socket))
end

for T in [BitArray, AbstractArray, UInt8]
for T in [BitArray, Vector{UInt8}, UInt8]
@eval Base.write(stream::ResponseStream, data::$T) = write(stream.socket, data)
end


function Base.readbytes!(stream::ResponseStream, data::Vector{UInt8}, sz)
while stream.state < BodyDone && nb_available(stream) < sz
wait(stream)
end
readbytes!(stream.buffer, data, sz)
end

function Base.readbytes(stream::ResponseStream)
function Base.read(stream::ResponseStream)
while stream.state < BodyDone
wait(stream)
end
Expand All @@ -140,7 +141,7 @@ function Base.readavailable(stream::ResponseStream)
while nb_available(stream) == 0
wait(stream)
end
readbytes(stream, nb_available(stream))
read(stream, nb_available(stream))
end

Base.close(stream::ResponseStream) = close(stream.socket)
Expand Down
13 changes: 7 additions & 6 deletions test/runtests.jl
@@ -1,3 +1,4 @@
using Compat
using Requests
using JSON
using Base.Test
Expand Down Expand Up @@ -133,14 +134,14 @@ data = json(post("http://httpbin.org/post",
filename = Base.source_path()

files = [
FileParam(readall(filename),"text/julia","file1","runtests.jl"),
FileParam(readstring(filename),"text/julia","file1","runtests.jl"),
FileParam(open(filename,"r"),"text/julia","file2","runtests.jl",true),
FileParam(IOBuffer(readall(filename)),"text/julia","file3","runtests.jl"),
FileParam(IOBuffer(readstring(filename)),"text/julia","file3","runtests.jl"),
]

res = post(URI("http://httpbin.org/post"); files = files)

filecontent = readall(filename)
filecontent = readstring(filename)
data = json(res)
@test data["files"]["file1"] == filecontent
@test data["files"]["file2"] == filecontent
Expand Down Expand Up @@ -194,7 +195,7 @@ let
write_chunked(stream, "cde")
write_chunked(stream, "")

response = JSON.parse(readall(stream))
response = JSON.parse(readstring(stream))
@test response["data"] == "abcde"
end

Expand All @@ -220,15 +221,15 @@ end
# Requires Docker and docker-machine (obtainable via Docker toolbox)
if get(ENV, "REQUESTS_TEST_PROXY", "0") == "1"
run(`docker-machine create -d virtualbox proxytest`)
cmds = readall(`docker-machine env proxytest`)
cmds = readstring(`docker-machine env proxytest`)
for line in split(cmds, '\n')
m = match(r"export (?<name>.*?)=(?<val>.*)", line)
m===nothing && continue
ENV[m[:name]] = m[:val]
end
@show ENV
run(`docker run --name squid -d --restart=always -p 3128:3128 quay.io/sameersbn/squid:3.3.8-2`)
ip = IPv4(readall(`docker-machine ip proxytest`))
ip = IPv4(readstring(`docker-machine ip proxytest`))
proxy_vars = ["http_proxy", "https_proxy"]
for var in proxy_vars
ENV[var] = "http://$ip:3128"
Expand Down

0 comments on commit 572c984

Please sign in to comment.