Skip to content

Commit

Permalink
Now reads the headers
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanpc committed Nov 17, 2012
1 parent b1f5ae2 commit 047b6ac
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
31 changes: 24 additions & 7 deletions handler.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@
(provide accept-and-handle)
(provide handle)

(require "./while.rkt")

(define (get-header in)
(define tmp-list (list))

(define current-header (read-line in))
(while (not (equal? current-header "\r"))
(set! tmp-list (append tmp-list (string-split current-header "\r")))
(set! current-header (read-line in)))
tmp-list)


(define (request-type req-head)
(list-ref (string-split req-head) 0))

(define (requested-location req-head)
(list-ref (string-split req-head) 1))

; Setup the connection handler.
(define (accept-and-handle listener)
(define-values (in out) (tcp-accept listener))
Expand All @@ -11,12 +29,11 @@

; The handler.
(define (handle in out)
(display in)
;(define request-headers (port->string in))
;(sync )
;(print (handle-evt? (eof-evt in)))
;(print (evt? (eof-evt in)))
;(display (format "~a~n" ))
(define headers (get-header in))

(display (format "~s~n" headers))
(display (format "~s~n" (string-split (list-ref headers 0))))

; Discart the request header (up to a blank line)
;(regexp-match #rx"(\r\n|^)\r\n" in)

Expand All @@ -26,4 +43,4 @@
(display "Content-Type: text/html\r\n" out)
(display "\r\n" out)
;(file->string file #:mode 'text)
(display "<h1>It works!</h1>" out)))
(display "<h1>It works!</h1>\r\n\r\n" out)))
11 changes: 11 additions & 0 deletions while.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(module while racket
(provide while)

; Credits: http://codeimmersion.i3ci.hampshire.edu/2009/10/15/a-scheme-while-loop/
(define-syntax (while stx)
(syntax-case stx ()
((_ condition expression ...)
#`(do ()
((not condition))
expression
...)))))

0 comments on commit 047b6ac

Please sign in to comment.