Skip to content

Commit

Permalink
Move stuff about; add LICENSE
Browse files Browse the repository at this point in the history
  • Loading branch information
dirk committed Nov 22, 2012
1 parent d96955b commit 7527b4e
Show file tree
Hide file tree
Showing 7 changed files with 274 additions and 270 deletions.
7 changes: 7 additions & 0 deletions LICENSE.md
@@ -0,0 +1,7 @@
Copyright (C) 2012 Dirk Gadsden

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
269 changes: 0 additions & 269 deletions lib/HTTP.jl

This file was deleted.

1 change: 0 additions & 1 deletion lib/parse.jl

This file was deleted.

119 changes: 119 additions & 0 deletions src/BasicServer.jl
@@ -0,0 +1,119 @@
module BasicServer
using Base
import Base.+
+(a::ASCIIString,b::ASCIIString) = strcat(a, b)

using HTTP

load("HTTP/src/BasicServer/Parser")

function bind(port, app)
println("Opening...")

sockfd = ccall(:open_any_tcp_port, Int32, (Ptr{Int16},), [int16(port)])
println("sockfd: "+string(sockfd))
if sockfd == -1
println("Error opening")
return
end

header = ""
lastline = ""

println("Serving...")

iter = 0

while true
println("iter: "+string(iter))

connectfd = ccall(:accept, Int32, (Int32, Ptr{Void}, Ptr{Void}), sockfd, C_NULL, C_NULL)
if connectfd == -1
println("Error accepting")
break
end

println("connectfd: "+string(connectfd))
iostream = fdio(connectfd)

#raw = readall(iostream)
raw = ""
nb = true
while nb
line = readline(iostream)
raw = raw + line
if line == "\r\n" || line == "\n"
nb = false
end
end

parts = split(raw, "\r")
raw = join(parts, "")
requests = vec(split(raw, "\n\n"))

#println(header)

#response_data = handle_request(strip(header))
response_data = ""
while length(requests) > 0
resp = handle_request(requests, app)
if resp != nothing
response_data = response_data + resp
end
end

write(iostream, response_data)
close(iostream)

ccall(:close, Int32, (Int32,), connectfd)

iter = iter + 1
end

end#bind

function handle_request(requests, app)
if length(requests) == 0
return nothing
end

raw_request = strip(shift(requests))
if strlen(raw_request) == 0
return nothing
end

request = HTTP.Request()
response = HTTP.Response()

request_line, raw_header = split(raw_request, "\n", 2)

method, path, version = Parser.parse_request_line(request_line)
request.method = method
request.path = path
request.version= version

request.headers = Parser.parse_header(raw_header)

if isequal(request.method, "POST") && count(requests) > 0
request.data = shift(requests)
end

not_found = "HTTP/1.1 404 Not found\r\n\r\nNot found\n"
internal_error = "HTTP/1.1 500 Server error\r\n\r\nInternal server error (no app)\n"

if isa(app, Function)
ret = app(request, response)
if isequal(ret, nothing)
return not_found
else
status = string(ret[1])
body = string(ret[2])
return "HTTP/1.1 "+status+"\r\n\r\n"+body
end
else
return internal_error
end
end


end

0 comments on commit 7527b4e

Please sign in to comment.