Skip to content

Commit

Permalink
Properly close connections when requested (#18)
Browse files Browse the repository at this point in the history
If the client uses HTTP/1.0 or "Connection: close" then the server
*must* close the connection after the response has been sent.
  • Loading branch information
skeeto committed Feb 18, 2018
1 parent be73a17 commit c252765
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion simple-httpd.el
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,11 @@ emacs -Q -batch -l simple-httpd.elc -f httpd-batch-start"

;; Networking code

(defun httpd--connection-close-p (request)
"Return non-nil if the client requested \"connection: close\"."
(or (equal '("close") (cdr (assoc "Connection" request)))
(equal '("HTTP/1.0") (cddr (assoc "GET" request)))))

(defun httpd--filter (proc chunk)
"Runs each time client makes a request."
(with-current-buffer (process-get proc :request-buffer)
Expand Down Expand Up @@ -435,7 +440,9 @@ emacs -Q -batch -l simple-httpd.elc -f httpd-batch-start"
(httpd--error-safe proc 404)
(condition-case error-case
(funcall servlet proc uri-path uri-query request)
(error (httpd--error-safe proc 500 error-case)))))))))))
(error (httpd--error-safe proc 500 error-case))))
(when (httpd--connection-close-p request)
(process-send-eof proc)))))))))

(defun httpd--log (server proc message)
"Runs each time a new client connects."
Expand Down

0 comments on commit c252765

Please sign in to comment.