Skip to content

Commit

Permalink
Use require "http/client", require "http/server" instead of `requ…
Browse files Browse the repository at this point in the history
…ire "net/http"`... Related to #279
  • Loading branch information
Ary Borenszweig committed Dec 3, 2014
1 parent 5daa4e1 commit b2f6acf
Show file tree
Hide file tree
Showing 29 changed files with 210 additions and 35 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
## Next

* **(breaking change)** Renamed `Json` to `JSON`, `Xml` to `XML` and `Yaml` to `YAML` to follow [a convention](https://github.com/manastech/crystal/issues/279).
* **(breaking change)** `require "foo"` always looks up in `CRYSTAL_PATH`. `require "./foo"` looks up relative to the requiring file.
* **(breaking change)** Renamed `Json` to `JSON`, `Xml` to `XML` and `Yaml` to `YAML` to follow [a convention](https://github.com/manastech/crystal/issues/279).
* **(breaking change)** To use HTTP types do, for example, `require "http/client"` instead of the old `require "net/http"`.
* Added `alias_method` macro (thanks @Exilor and @jtomschroeder).
* Added some `Complex` number methods and many math methods, refactors and specs (thanks @scidom).
* Inheriting generic classes is now possible.
Expand Down
2 changes: 1 addition & 1 deletion libs/oauth/oauth.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require "net/http"
require "http/client"
require "uri"
require "cgi"
require "secure_random"
Expand Down
2 changes: 1 addition & 1 deletion libs/oauth2/oauth2.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require "cgi"
require "net/http"
require "http/client"
require "json"
require "./**"
2 changes: 1 addition & 1 deletion samples/http_server.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require "net/http/server"
require "http/server"

server = HTTP::Server.new 8080, do |request|
HTTP::Response.ok "text/plain", "Hello world!"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require "net/http"
require "http/client"

module HTTP
{% for method in %w(get post put head delete patch) %}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require "spec"
require "net/http/common"
require "http/headers"

describe HTTP::Headers do
it "is empty" do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require "spec"
require "net/http"
require "http/request"

module HTTP
describe Request do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require "spec"
require "net/http"
require "http/response"

module HTTP
describe Response do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require "spec"
require "net/http/server"
require "http/server"

describe HTTP::WebSocketHandler do
it "returns not found if the request is not an websocket upgrade" do
Expand Down
10 changes: 10 additions & 0 deletions spec/std/http/server_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require "http/server"

module HTTP
typeof(begin
server = Server.new(8080) { |req| Response.ok("text/plain", "OK") }
server.listen
server.listen_fork(workers: 2)
server.close
end)
end
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
require "spec"
require "net/http/websocket"
require "http/web_socket"

def packet(bytes)
slice = Slice(UInt8).new(bytes.length) { |i| bytes[i].to_u8 }
slice.pointer(bytes.length)
end

describe WebSocket do
describe HTTP::WebSocket do
describe "receive" do
it "can read a small text packet" do
data = packet([0x81, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f])
io = PointerIO.new(pointerof(data))
ws = WebSocket.new(io)
ws = HTTP::WebSocket.new(io)

buffer = Slice(UInt8).new(64)
result = ws.receive(buffer)
Expand All @@ -25,7 +25,7 @@ describe WebSocket do
data = packet([0x81, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f,
0x81, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f])
io = PointerIO.new(pointerof(data))
ws = WebSocket.new(io)
ws = HTTP::WebSocket.new(io)

buffer = Slice(UInt8).new(3)

Expand All @@ -48,7 +48,7 @@ describe WebSocket do
data = packet([0x81, 0x85, 0x37, 0xfa, 0x21, 0x3d, 0x7f, 0x9f, 0x4d, 0x51, 0x58,
0x81, 0x85, 0x37, 0xfa, 0x21, 0x3d, 0x7f, 0x9f, 0x4d, 0x51, 0x58])
io = PointerIO.new(pointerof(data))
ws = WebSocket.new(io)
ws = HTTP::WebSocket.new(io)

buffer = Slice(UInt8).new(3)

Expand All @@ -72,7 +72,7 @@ describe WebSocket do
0x01, 0x03, 0x48, 0x65, 0x6c, 0x80, 0x02, 0x6c, 0x6f])

io = PointerIO.new(pointerof(data))
ws = WebSocket.new(io)
ws = HTTP::WebSocket.new(io)

buffer = Slice(UInt8).new(10)

Expand All @@ -94,7 +94,7 @@ describe WebSocket do
it "read ping packet" do
data = packet([0x89, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f])
io = PointerIO.new(pointerof(data))
ws = WebSocket.new(io)
ws = HTTP::WebSocket.new(io)

buffer = Slice(UInt8).new(64)
result = ws.receive(buffer)
Expand All @@ -109,7 +109,7 @@ describe WebSocket do
0x89, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f,
0x80, 0x02, 0x6c, 0x6f])
io = PointerIO.new(pointerof(data))
ws = WebSocket.new(io)
ws = HTTP::WebSocket.new(io)

buffer = Slice(UInt8).new(64)

Expand All @@ -133,9 +133,9 @@ describe WebSocket do
end

it "read long packet" do
data = File.read("#{__DIR__}/../../data/websocket_longpacket.bin").cstr
data = File.read("#{__DIR__}/../data/websocket_longpacket.bin").cstr
io = PointerIO.new(pointerof(data))
ws = WebSocket.new(io)
ws = HTTP::WebSocket.new(io)

buffer = Slice(UInt8).new(2048)

Expand Down
104 changes: 104 additions & 0 deletions spec/std/iterator_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
require "spec"
require "iterator"

describe Iterator do
describe "ArrayIterator" do
it "does next" do
a = [1, 2, 3]
iterator = a.iterator
iterator.next.should eq(1)
iterator.next.should eq(2)
iterator.next.should eq(3)
expect_raises StopIteration do
iterator.next
end
end
end

describe "RangeIterator" do
it "does next with inclusive range" do
a = 1..3
iterator = a.iterator
iterator.next.should eq(1)
iterator.next.should eq(2)
iterator.next.should eq(3)
expect_raises StopIteration do
iterator.next
end
end

it "does next with exclusive range" do
r = 1...3
iterator = r.iterator
iterator.next.should eq(1)
iterator.next.should eq(2)
expect_raises StopIteration do
iterator.next
end
end
end

describe "IntIterator" do
it "does next with times" do
n = 3
iterator = n.times
iterator.next.should eq(0)
iterator.next.should eq(1)
iterator.next.should eq(2)
expect_raises StopIteration do
iterator.next
end
end
end

describe "map" do
it "does map with Range iterator" do
(1..3).iterator.map { |x| x * 2 }.to_a.should eq([2, 4, 6])
end
end

describe "select" do
it "does select with Range iterator" do
(1..3).iterator.select { |x| x >= 2 }.to_a.should eq([2, 3])
end
end

describe "reject" do
it "does reject with Range iterator" do
(1..3).iterator.reject { |x| x >= 2 }.to_a.should eq([1])
end
end

describe "take" do
it "does take with Range iterator" do
(1..3).iterator.take(2).to_a.should eq([1, 2])
end

it "does take with more than available" do
(1..3).iterator.take(10).to_a.should eq([1, 2, 3])
end
end

describe "skip" do
it "does skip with Range iterator" do
(1..3).iterator.skip(2).to_a.should eq([3])
end
end

describe "zip" do
it "does skip with Range iterator" do
r1 = (1..3).iterator
r2 = (4..6).iterator
r1.zip(r2).to_a.should eq([{1, 4}, {2, 5}, {3, 6}])
end
end

it "combines many iterators" do
(1..100).iterator
.select { |x| 50 <= x < 60 }
.map { |x| x * 2 }
.take(3)
.to_a
.should eq([100, 102, 104])
end
end
37 changes: 37 additions & 0 deletions spec/std/markdown/markdown_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require "spec"
require "markdown"

module Markdown
def self.expect_html(input, output)
it "renders markdown #{input.inspect}" do
render_html(input).should eq(output)
end
end

describe "Markdown" do
expect_html "Hello", "<p>Hello</p>"
expect_html "Hello\nWorld", "<p>Hello\nWorld</p>"
expect_html "Hello\n\nWorld", "<p>Hello</p>\n<p>World</p>"
expect_html "Hello\n\n\n\n\nWorld", "<p>Hello</p>\n<p>World</p>"
expect_html "Hello *world*", "<p>Hello <em>world</em></p>"
expect_html "Hello _world_", "<p>Hello <em>world</em></p>"
expect_html "Hello *world", "<p>Hello *world</p>"
expect_html "Hello *world\nBye *world", "<p>Hello *world\nBye *world</p>"
expect_html "Hello * world *", "<p>Hello * world *</p>"
expect_html "Hello **world**", "<p>Hello <strong>world</strong></p>"
expect_html "Hello __world__", "<p>Hello <strong>world</strong></p>"
expect_html "Hello **world", "<p>Hello **world</p>"
expect_html "Hello **world\nBye **world", "<p>Hello **world\nBye **world</p>"
expect_html "Hello `world`", "<p>Hello <code>world</code></p>"
expect_html "Hello `world`\nBye `world`", "<p>Hello <code>world</code>\nBye <code>world</code></p>"
expect_html "Hello `world", "<p>Hello `world</p>"
expect_html "Hello *`world`*", "<p>Hello <em><code>world</code></em></p>"
expect_html "Hello *`world`", "<p>Hello *<code>world</code></p>"

1.upto(6) do |count|
expect_html "#{"#" * count} Hello", "<h#{count}>Hello</h#{count}>"
end
expect_html "####### Hello", "<h6># Hello</h6>"
# expect_html "# One\n# Two", "<h1>One</h1>\n<h1>Two</h1>"
end
end
19 changes: 19 additions & 0 deletions spec/std/toml/lexer_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require "spec"
require "toml"

def it_lexes_toml(string, expected_type)
it "lexes #{string}" do
lexer = Toml::Lexer.new string
token = lexer.next_token
token.type.should eq(expected_type)
end
end

describe "Toml::Lexer" do
it_lexes_toml "", :EOF
it_lexes_toml "[", :"["
it_lexes_toml "]", :"]"
it_lexes_toml ".", :"."
it_lexes_toml "=", :"="
it_lexes_toml ",", :","
end
2 changes: 1 addition & 1 deletion src/compiler/crystal/compiler.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "option_parser"
require "file_utils"
require "socket"
require "net/http/common/common"
require "http/common"
require "colorize"
require "tempfile"

Expand Down
2 changes: 1 addition & 1 deletion src/evented_io.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "fiber"
require "event"
require "net/http/server/server"
require "http/server"

def spawn(&block)
Fiber.new(&block).resume
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ require "socket"
require "uri"
require "cgi"
require "base64"
require "../common/common"
require "../common"

class HTTP::Client
getter host
Expand Down
9 changes: 6 additions & 3 deletions src/net/http/common/common.cr → src/http/common.cr
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
require "./*"

module HTTP
def self.parse_headers_and_body(io)
headers = HTTP::Headers.new
headers = Headers.new

while line = io.gets
if line == "\r\n" || line == "\n"
Expand Down Expand Up @@ -42,3 +40,8 @@ module HTTP
io << body if body
end
end

require "./request"
require "./response"
require "./headers"

File renamed without changes.
2 changes: 2 additions & 0 deletions src/http/http.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require "./**"

2 changes: 1 addition & 1 deletion src/net/http/common/request.cr → src/http/request.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class HTTP::Request
getter body
getter version

def initialize(@method : String, @path, @headers = HTTP::Headers.new : HTTP::Headers, @body = nil, @version = "HTTP/1.1")
def initialize(@method : String, @path, @headers = Headers.new : Headers, @body = nil, @version = "HTTP/1.1")
if body = @body
@headers["Content-Length"] = body.bytesize.to_s
elsif @method == "POST" || @method == "PUT"
Expand Down
Loading

0 comments on commit b2f6acf

Please sign in to comment.