Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
akkartik committed Jun 6, 2012
1 parent b9a6c63 commit ce74bff
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions lib/srv.arc
Expand Up @@ -46,23 +46,23 @@
(errsafe (handle-request-1 s))))

(def handle-request-1 (s)
(with ((i o ip) (socket-accept s)
(with ((in out ip) (socket-accept s)
th1 nil th2 nil)
(++ requests*)
(let ip-wrapper (fn args
(if args
(= ip car.args)
ip))
(= th1 (thread
(after (handle-request-thread i o ip-wrapper)
(close i o)
(after (handle-request-thread in out ip-wrapper)
(close in out)
(kill-thread th2))))
(= th2 (thread
(sleep threadlife*)
(unless (dead th1)
(prn "srv thread took too long for " ip))
(kill-thread th1)
(force-close i o))))))
(force-close in out))))))

; Returns true if ip has made req-limit* requests in less than
; req-window* seconds. If an ip is throttled, only 1 request is
Expand Down Expand Up @@ -107,29 +107,29 @@
(ip-wrapper))))

(wipe show-requests*)
(def handle-request-thread (i o ip-wrapper)
(def handle-request-thread (in out ip-wrapper)
(with (nls 0 lines nil line nil responded nil t0 (msec))
(after
(whilet c (unless responded (readc i))
(whilet c (unless responded (readc in))
(if srv-noisy* (pr c))
(if (is c #\newline)
(if (is (++ nls) 2)
(let (type op args n cooks ctype) (parseheader (rev lines))
(let (type op args clen cooks ctype) (parseheader (rev lines))
(if show-requests* (prn lines))
(unless (abusive-ip (proxy-ip ip-wrapper lines))
(let t1 (msec)
(case type
get (respond o op args cooks 0 i "" (ip-wrapper))
post (handle-post i o op args n cooks ctype (ip-wrapper))
(respond-err o "Unknown request: " (car lines)))
get (respond out op args cooks 0 in "" (ip-wrapper))
post (handle-post in out op args clen cooks ctype (ip-wrapper))
(respond-err out "Unknown request: " (car lines)))
(log-request type op args cooks (ip-wrapper) t0 t1)))
(set responded))
(do (push (string (rev line)) lines)
(wipe line)))
(unless (is c #\return)
(push c line)
(= nls 0))))
(close i o)))
(close in out)))
(harvest-fnids))

(def log-request (type op args cooks ip t0 t1)
Expand Down Expand Up @@ -242,17 +242,17 @@ Connection: close"))
(writeb b str))))
(respond-err str unknown-msg*))))))

(def handle-post (i o op args n cooks ctype ip)
(def handle-post (in out op args clen cooks ctype ip)
(if srv-noisy* (pr "Post Contents: "))
(if (no n)
(respond-err o "Post request without Content-Length.")
(let body (string:readchars n i)
(if (no clen)
(respond-err out "Post request without Content-Length.")
(let body (string:readchars clen in)
(if srv-noisy* (pr body "\r\n\r\n"))
(respond o op (+ args
(if (~begins downcase.ctype "multipart/form-data")
parseargs.body
(parse-multipart-args multipart-boundary.ctype body)))
cooks n ctype i ip))))
(respond out op (+ args
(if (~begins downcase.ctype "multipart/form-data")
parseargs.body
(parse-multipart-args multipart-boundary.ctype body)))
cooks clen ctype in ip))))

(def parse-multipart-args(boundary body)
(let indices (find-all boundary body)
Expand Down

0 comments on commit ce74bff

Please sign in to comment.