Skip to content

Commit

Permalink
elnode-proxy/web-hdr-hash->alist: new function cleans up web-hdrs->alist
Browse files Browse the repository at this point in the history
elnode-proxy/web-client: new function cleans up the client side
elnode/proxy-route: use elnode/con-get properly
  • Loading branch information
nicferrier committed Jul 28, 2014
1 parent 7c32a0b commit 6a2d743
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 33 deletions.
61 changes: 30 additions & 31 deletions elnode-proxy.el
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,15 @@
(require 'kv)
(require 'cl) ; for destructuring-bind

(defun elnode--web->elnode-hdr (hdr httpcon)
"Send the HDR from the web HTTP request to Elnode's HTTPCON."
(let ((headers
(-filter
(lambda (hdr-pair)
(unless (member
(downcase (symbol-name (car hdr-pair)))
'("status-code" "status-string" "status-version"))
(cons (symbol-name (car hdr-pair))
(cdr hdr-pair))))
(kvhash->alist hdr))))
(apply 'elnode-http-start httpcon 200 headers)))
(defun elnode-proxy/web-hdr-hash->alist (web-hdr)
(-filter
(lambda (hdr-pair)
(unless (member
(downcase (symbol-name (car hdr-pair)))
'("status-code" "status-string" "status-version"))
(cons (symbol-name (car hdr-pair))
(cdr hdr-pair))))
(kvhash->alist web-hdr)))

(defun elnode--proxy-x-forwarded-for (httpcon)
"Return an X-Forwaded-For header."
Expand All @@ -35,6 +32,15 @@
(concat hdr (format ", %s" ipaddr))
ipaddr)))

(defun elnode-proxy/web-client (httpc header data httpcon)
(unless (elnode/con-get httpcon :elnode-proxy-header-sent)
(let ((headers (elnode-proxy/web-hdr-hash->alist header)))
(apply 'elnode-http-start httpcon 200 headers))
(elnode/con-put httpcon :elnode-proxy-header-sent t))
(if (eq data :done)
(elnode-http-return httpcon)
(elnode-http-send-string httpcon data)))

(defun elnode-proxy-do (httpcon url)
"Do proxying to URL on HTTPCON.
Expand All @@ -61,24 +67,17 @@ specified path and query."
(cons "params" params)))
(web-url (s-format url 'aget params-alist))
hdr-sent)
(process-put
httpcon
:elnode-child-process
(web-http-call
method
(lambda (httpc hdr data)
(unless hdr-sent
(elnode--web->elnode-hdr hdr httpcon)
(setq hdr-sent t))
(if (eq data :done)
(elnode-http-return httpcon)
(elnode-http-send-string httpcon data)))
:mode 'stream
:url web-url
:extra-headers
`(("X-Forwarded-For"
. ,(elnode--proxy-x-forwarded-for httpcon))
("X-Proxy-Client" . "elnode/web"))))))
(let ((web-con
(web-http-call
method
(lambda (httpc hdr data)
(elnode-proxy/web-client httpc hdr data httpcon))
:mode 'stream
:url web-url
:extra-headers
`(("X-Forwarded-For" . ,(elnode--proxy-x-forwarded-for httpcon))
("X-Proxy-Client" . "elnode/web")))))
(elnode/con-put httpcon :elnode-child-process web-con))))

(defun elnode-proxy-bounce (httpcon handler host-port)
"Bounce this request.
Expand Down Expand Up @@ -221,7 +220,7 @@ proxy connection."

(defun elnode/proxy-route (httpcon service handler path)
"Proxies a particular route from `elnode-route'."
(let* ((server (process-get httpcon :server))
(let* ((server (elnode/con-get httpcon :server))
(p2 path)
(maps (process-get server :elnode-service-map))
(port
Expand Down
8 changes: 6 additions & 2 deletions elnode.el
Original file line number Diff line number Diff line change
Expand Up @@ -865,12 +865,16 @@ port number of the connection."
(elnode-msg :info "filter: immediate defer on %s" process)
(funcall (cdr signal-value) process))))
('t
(unless (elnode/con-get process :elnode-http-started)
(unless (or (elnode/con-get process :elnode-child-process)
(elnode/con-get process :elnode-http-started))
(elnode-msg :info "filter: default handling %S" signal-value)
(process-send-string process (elnode--format-response 500)))))
(if (and (not (elnode/con-get process :elnode-http-started))
(if (and (not (or
(elnode/con-get process :elnode-http-started)
(elnode/con-get process :elnode-child-process)))
(not (elnode/con-get process :elnode-deferred)))
(process-send-string process (elnode--format-response 500))
;; Else
(when (elnode/con-get process :elnode-finished)
(unwind-protect
(progn
Expand Down

0 comments on commit 6a2d743

Please sign in to comment.