Skip to content

Allow to manipulate request body, headers and method before a request is proxied #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions revproxy-ns-connchan-procs.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ namespace eval ::revproxy::ns_connchan {
{-exception_callback "::revproxy::exception"}
{-url_rewrite_callback "::revproxy::rewrite_url"}
{-backend_reply_callback ""}
-method
-content
-contentfile
-queryHeaders
} {
#
# Inject a "connection close" instruction to avoid persistent
Expand All @@ -37,7 +41,6 @@ namespace eval ::revproxy::ns_connchan {
#
# We might have to take more precautions for WebSockets here.
#
set queryHeaders [ns_conn headers]
ns_set iupdate $queryHeaders connection close

#ns_log notice queryHeaders=[ns_set array $queryHeaders]
Expand All @@ -63,7 +66,7 @@ namespace eval ::revproxy::ns_connchan {
#
set backendChan [ns_connchan open \
{*}$unixSocketArg \
-method [ns_conn method] \
-method $method \
-headers $queryHeaders \
-timeout $timeout \
-version [ns_conn version] \
Expand All @@ -73,20 +76,18 @@ namespace eval ::revproxy::ns_connchan {
#
set contentLength [ns_set iget $queryHeaders content-length {}]
if {$contentLength ne ""} {
set contentfile [ns_conn contentfile]
set chunk 16000
if {$contentfile eq ""} {
#
# string content
#
set data [ns_conn content -binary]
set length [string length $data]
set length $contentLength
set i 0
set j [expr {$chunk -1}]
while {$i < $length} {
log notice "upstream: send max $chunk bytes from string to $backendChan " \
"(length $contentLength)"
ns_connchan write -buffered $backendChan [string range $data $i $j]
ns_connchan write -buffered $backendChan [string range $content $i $j]
incr i $chunk
incr j $chunk
}
Expand Down
12 changes: 6 additions & 6 deletions revproxy-ns-http-procs.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ namespace eval ::revproxy::ns_http {
{-validation_callback ""}
{-exception_callback "::revproxy::exception"}
{-backend_reply_callback ""}
-method
-content
-contentfile
-queryHeaders
} {
#
# Now perform the transmission... but before this, call the
Expand All @@ -24,25 +28,21 @@ namespace eval ::revproxy::ns_http {
{*}$validation_callback -url $url
}

set queryHeaders [ns_conn headers]
set binary false
set extraArgs {}

switch [ns_conn method] {
switch $method {
PUT -
POST {
set contentType [ns_set iget $queryHeaders content-type]
if {$contentType ne ""
&& [ns_encodingfortype $contentType] eq "binary"} {
set binary true
}
set contentfile [ns_conn contentfile]
if {$contentfile ne ""} {
lappend extraArgs -body_file $contentfile
} elseif {$binary} {
lappend extraArgs -body [ns_conn content -binary]
} else {
lappend extraArgs -body [ns_conn content]
lappend extraArgs -body $content
}
}
default {}
Expand Down
34 changes: 33 additions & 1 deletion revproxy-procs.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ namespace eval ::revproxy {
#
# Serve the requested file from an upstream server, which might be
# an HTTP or HTTPS server.
#
# By default, headers method and request body will be those of the
# current request. One can craft a custom request to be forwarded
# to the backend overriding these parameters via the "content" /
# "contentfile" and "queryHeaders" flags.
#

nsf::proc upstream {
when
Expand All @@ -45,6 +51,9 @@ namespace eval ::revproxy {
{-url_rewrite_callback "::revproxy::rewrite_url"}
{-backend_reply_callback ""}
{-backendconnection ""}
{-content ""}
{-contentfile ""}
{-queryHeaders ""}
} {

if {$backendconnection eq ""} {
Expand Down Expand Up @@ -130,7 +139,9 @@ namespace eval ::revproxy {
# Get header fields from request, add x-forwarded-for,
# x-forwarded-proto, and x-ssl-request (if appropriate).
#
set queryHeaders [ns_conn headers]
if {$queryHeaders eq ""} {
set queryHeaders [ns_conn headers]
}

set XForwardedFor [split [ns_set iget $queryHeaders "x-forwarded-for" ""] " ,"]
set XForwardedFor [lmap e $XForwardedFor {if {$e eq ""} continue}]
Expand All @@ -143,6 +154,23 @@ namespace eval ::revproxy {
ns_set iupdate $queryHeaders x-ssl-request 1
}

set contentType [ns_set iget $queryHeaders content-type]
set binary [expr {$contentType ne "" && [ns_encodingfortype $contentType] eq "binary"}]

if {$content ne ""} {
ns_set iupdate $queryHeaders content-length [string length $content]
} elseif {$binary} {
set content [ns_conn content -binary]
} else {
set content [ns_conn content]
}

if {$contentfile ne ""} {
ns_set iupdate $queryHeaders content-length [file size $contentfile]
} else {
set contentfile [ns_conn contentfile]
}

#
# Finally, start the transmission to the backend via the
# configured means.
Expand All @@ -155,6 +183,10 @@ namespace eval ::revproxy {
-validation_callback $validation_callback \
-exception_callback $exception_callback \
-backend_reply_callback $backend_reply_callback \
-method $when \
-content $content \
-contentfile $contentfile \
-queryHeaders $queryHeaders \
]
}

Expand Down