Hash :
224277a6
Author :
Thomas de Grivel
Date :
2017-06-25T23:57:57
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
(in-package :thot)
(setf (debug-p :thot) nil)
(eval-when (:compile-toplevel :load-toplevel :execute)
(unless (boundp '+crlf+)
(defconstant +crlf+
(coerce '(#\Return #\Newline) 'string))))
;; Request
(defclass request ()
((stream :initarg :stream
:reader request-stream%
:type stream)
(method :initarg :method
:accessor request-method%
:type symbol)
(target :initarg :target
:accessor request-target%
:type string)
(http-version :initarg :http-version
:accessor request-http-version%
:type string)
(headers :initform (make-hash-table :test 'equalp :size 32)
:reader request-headers%
:type hash-table)
(uri :accessor request-uri%
:type string)
(query :initform nil
:accessor request-query%
:type string)))
(defvar *request*)
(defun request-stream (&optional (request *request*))
(declare (type request request))
(request-stream% request))
(defun request-method (&optional (request *request*))
(declare (type request request))
(request-method% request))
(defun request-target (&optional (request *request*))
(declare (type request request))
(request-target% request))
(defun request-http-version (&optional (request *request*))
(declare (type request request))
(request-http-version% request))
(defun request-headers (&optional (request *request*))
(declare (type request request))
(request-headers% request))
(defun split-request-uri-and-query (request)
(declare (type request request))
(let* ((target (request-target% request))
(target-? (position #\? target))
(uri (if target-?
(subseq target 0 target-?)
target))
(query (if target-?
(subseq target target-?)
nil)))
(setf (request-uri% request) uri
(request-query% request) query)))
(defun request-uri (&optional (request *request*))
(declare (type request request))
(unless (slot-boundp request 'uri)
(split-request-uri-and-query request))
(slot-value request 'uri))
(defun request-query (&optional (request *request*))
(declare (type request request))
(unless (slot-boundp request 'query)
(split-request-uri-and-query request))
(slot-value request 'query))
(defun request-header (header-name &optional (request *request*))
(declare (type request request))
(gethash header-name (request-headers% request)))
(defsetf request-header (header-name &optional (request '*request*)) (value)
`(setf (gethash ,header-name (request-headers ,request)) ,value))
(defun request-content-length (&optional (request *request*))
(parse-integer (request-header "Content-Length" request)))
;; HTTP parser
(defmacro with-readers-for (stream definitions &body body)
(declare (type symbol stream))
(flet ((reader (definition)
(destructuring-bind (name (element) &rest body) definition
(declare (type symbol name element))
(let ((state (gensym "STATE-")))
`(,name ()
(multiple-value-bind (,element ,state) (read ,stream)
(case ,state
((nil) ,@body)
((:eof) :eof)
((:non-blocking) #',name)
(otherwise (error 'stream-input-error
:stream ,stream)))))))))
`(labels ,(mapcar #'reader definitions)
,@body)))
(defun request-reader (request reply cont)
"Returns a HTTP request reader function which itself returns either :
:EOF if end of stream was reached, or
a closure if read would block, or
the return value of CONT, which should be either :
:KEEP-ALIVE if the connection is to be kept alive for next request, or
NIL if the request stream is to be closed after reply."
(let ((stream (request-stream request))
(buffer (string-output-stream))
(name "")
(value ""))
(flet ((get-buffer ()
(prog1 (string-output-stream-string buffer)
(sequence-output-stream-reset buffer))))
(with-readers-for stream
((method (char)
(cond ((char= #\Space char)
(setf (request-method% request) (get-buffer))
(target))
(t (write buffer char)
(method))))
(target (char)
(cond ((char= #\Space char)
(setf (request-target% request) (get-buffer))
(version))
(t (write buffer char)
(target))))
(version (char)
(cond ((char= #\Return char)
(setf (request-http-version% request) (get-buffer))
(version-lf))
(t (write buffer char)
(version))))
(version-lf (char)
(cond ((char= #\Newline char)
(when (debug-p (or :thot :http))
(format t "~&thot: ~A ~A ~A~%"
(request-method% request)
(request-target% request)
(request-http-version% request)))
(next-header))
(t (error "Missing request line LF"))))
(next-header (char)
(cond ((char= #\Return char) (end-of-headers))
(t (write buffer char)
(header-name))))
(header-name (char)
(cond ((char= #\: char)
(setq name (get-buffer))
(header-spaces))
(t (write buffer char)
(header-name))))
(header-spaces (char)
(cond ((char= #\Space char) (header-spaces))
(t (write buffer char)
(header-value))))
(header-value (char)
(cond ((char= #\Return char)
(setq value (get-buffer))
(header-lf))
(t (write buffer char)
(header-value))))
(header-lf (char)
(cond ((char= #\Newline char)
(when (debug-p (or :thot :http))
(format t "~&thot: ~A: ~A~%" name value))
(setf (request-header name request) value)
(next-header))
(t (error "Missing header LF"))))
(end-of-headers (char)
(cond ((char= #\Newline char) (funcall cont request reply))
(t (error "Missing end of headers LF")))))
#'method))))
;; Reply
(defclass reply ()
((status :initform nil
:accessor reply-status%)
(headers :initform nil
:accessor reply-headers%)
(headers-sent :initform nil
:accessor reply-headers-sent%
:type boolean)
(cont-queue :initform (make-instance 'queue
:element-type 'function
:size 40)
:accessor reply-cont-queue%
:type queue)))
(defvar *reply*)
(defun reply-status (&optional (reply *reply*))
(declare (type reply reply))
(reply-status% reply))
(defsetf reply-status (&optional (reply '*reply*)) (value)
`(setf (reply-status% ,reply) ,value))
(defun reply-headers (&optional (reply *reply*))
(declare (type reply reply))
(reply-headers% reply))
(defsetf reply-headers (&optional (reply '*reply*)) (value)
`(setf (reply-headers% ,reply) ,value))
(defun reply-header (name &optional (reply *reply*)) (declare (type reply reply))
(let ((header (assoc name (reply-headers reply) :test #'string-equal)))
(when header
(rest header))))
(defun reply-headers-sent (&optional (reply *reply*))
(declare (type reply reply))
(reply-headers-sent% reply))
(defsetf reply-headers-sent (&optional (reply '*reply*)) (value)
`(setf (reply-headers-sent% ,reply) ,value))
(defun reply-cont-queue (&optional (reply *reply*))
(declare (type reply reply))
(reply-cont-queue% reply))
(defsetf reply-cont-queue (&optional (reply '*reply*)) (value)
`(setf (reply-cont-queue% ,reply) ,value))
(defun reply-content-length (&optional (reply *reply*))
(declare (type reply reply))
(parse-integer (reply-header 'content-length reply)))
(defun reply-stream ()
(request-stream))
(defun status (line)
(let ((status (reply-status)))
(when status
(error 'status-already-sent status line)))
(setf (reply-status) line)
(let ((stream (reply-stream)))
(labels ((version (&optional (start 0))
(multiple-value-bind (count state)
(write-sequence stream (request-http-version) :start start)
(case state
((nil) (space))
((:eof) (error 'stream-end-error :stream stream))
((:non-blocking) (write (reply-cont-queue)
(lambda ()
(version (+ start count)))))
(otherwise (error 'stream-output-error :stream stream)))))
(space ()
(case (write stream #\Space)
((nil) (line))
((:eof) (error 'stream-end-error :stream stream))
((:non-blocking) (write (reply-cont-queue) #'space))
(otherwise (error 'stream-output-error :stream stream))))
(line (&optional (start 0))
(multiple-value-bind (count state)
(write-sequence stream line :start start)
(case state
((nil) (crlf))
((:eof) (error 'stream-end-error :stream stream))
((:non-blocking) (write (reply-cont-queue)
(lambda () (line (+ start count)))))
(otherwise (error 'stream-output-error :stream stream)))))
(crlf (&optional (start 0))
(multiple-value-bind (count state)
(write-sequence stream +crlf+)
(case state
((nil) nil)
((:eof) (error 'stream-end-error :stream stream))
((:non-blocking) (write (reply-cont-queue)
(lambda () (crlf (+ start count)))))
(otherwise (error 'stream-output-error
:stream stream))))))
(if (= 0 (queue-length (reply-cont-queue)))
(version)
(write (reply-cont-queue) #'version)))))
(defun header (line)
(unless (reply-status)
(status "200 OK"))
(let ((headers (reply-headers)))
(if (endp headers)
(setf (reply-headers) (list line))
(loop
(when (endp (rest headers))
(setf (rest headers) (list line))
(return))
(pop headers))))
(let ((stream (request-stream)))
(labels ((line (&optional (start 0))
(multiple-value-bind (count state)
(write-sequence stream line)
(case state
((nil) (crlf))
((:eof) (error 'stream-end-error :stream stream))
((:non-blocking) (write (reply-cont-queue)
(lambda () (line (+ start count)))))
(otherwise (error 'stream-output-error
:stream stream)))))
(crlf (&optional (start 0))
(multiple-value-bind (count state)
(write-sequence stream +crlf+)
(case state
((nil) nil)
((:eof) (error 'stream-end-error :stream stream))
((:non-blocking) (write (reply-cont-queue)
(lambda () (crlf (+ start count)))))
(otherwise (error 'stream-output-error
:stream stream))))))
(if (= 0 (queue-length (reply-cont-queue)))
(line)
(write (reply-cont-queue) #'line)))))
(defun end-headers ()
(unless (reply-headers-sent)
(setf (reply-headers-sent) t)
(header "")))
(defun content (string)
(end-headers)
(let ((stream (reply-stream)))
(labels ((content-string (&optional (start 0))
(multiple-value-bind (count state)
(write-sequence (request-stream) string)
(case state
((nil) nil)
(:eof) (error 'stream-end-error :stream stream)
((:non-blocking) (write (reply-cont-queue)
(lambda ()
(content-string (+ start count)))))
(otherwise (error 'stream-output-error
:stream stream))))))
(if (= 0 (queue-length (reply-cont-queue)))
(content-string)
(write (reply-cont-queue) #'content-string)))))
(defun 404-not-found ()
(status "404 Not found")
(header "Content-Type: text/plain")
(content (format nil "404 Not found
The requested url ~S was not found on this server."
(request-target))))
(defun 404-not-found-handler ()
'404-not-found)
(defvar *url-handlers*
'(404-not-found-handler))
(defun request-cont (request reply)
(let ((handlers *url-handlers*)
(*request* request)
(*reply* reply))
(loop
(when (endp handlers)
(return))
(let* ((handler-func (pop handlers))
(handler (funcall handler-func)))
(when handler
(when (debug-p (or :thot :http))
(format t "~&~S -> ~S~%" handler-func handler))
(funcall handler)
(flush (request-stream% request))
(return))))
(if (string-equal "keep-alive" (request-header 'connection))
:keep-alive
nil)))
(defvar *stop* nil)
(defun request-loop (stream)
(loop
(when *stop*
(return))
(let* ((request (make-instance 'request :stream stream))
(reply (make-instance 'reply))
(result (funcall (request-reader request reply #'request-cont))))
(unless (eq :keep-alive result)
(return)))))
(defvar *acceptor-loop*)
(defvar *host*)
(defvar *port*)
(defun start (&key (host "0.0.0.0") (port 8000))
(setq *stop* nil)
(let ((*host* host)
(*port* port))
(socket:with-socket (fd socket:+af-inet+
socket:+sock-stream+
0)
(socket:bind-inet fd host port)
(socket:listen fd 128)
(funcall *acceptor-loop* fd))))
(defun set-nonblocking (fd)
(let ((flags (fcntl:getfl fd)))
(fcntl:setfl fd (logior fcntl:+o-nonblock+ flags))))