You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: README.md
+13-4
Original file line number
Diff line number
Diff line change
@@ -358,6 +358,12 @@ default. You may choose to forward part or all of the following data:
358
358
or the `$MONOHOOK_FORWARD_REQUEST_BODY` environment variable.
359
359
360
360
If enabled, the request body will be piped to the command's standard input.
361
+
362
+
If the `-Y, --cache-request-body` command-line flag or the
363
+
`$MONOHOOK_CACHE_REQUEST_BODY` environment variable is also enabled, the
364
+
request body will be read immediately and cached for when the hook executes
365
+
(which might take a while depending on concurrency settings). Otherwise, the
366
+
request will wait to complete until the hook can actually execute.
361
367
* HTTP request headers with the `-H, --forward-request-headers` command-line
362
368
flag or the `$MONOHOOK_FORWARD_REQUEST_HEADERS` environment variable.
363
369
@@ -391,10 +397,12 @@ $> cat body.txt
391
397
0123456789
392
398
```
393
399
394
-
> Note that forwarding the request body means that the execution of the command
395
-
> will not end before the HTTP request body has been completely consumed. This
396
-
> may have a performance impact. For example, with a concurrency of 1, each
397
-
> request will have to wait for the previous one to upload its data.
400
+
> Note that forwarding the request body without caching it (using the `-Y,
401
+
> --cache-request-body` command-line flag or the `$MONOHOOK_CACHE_REQUEST_BODY`
402
+
> environment variable) means that the execution of the command will not end
403
+
> before the HTTP request body has been completely consumed. This may have a
404
+
> performance impact. For example, with a concurrency of 1, each request will
405
+
> have to wait for the previous one to upload its data.
398
406
399
407
### Port number
400
408
@@ -413,6 +421,7 @@ Usage:
413
421
Options:
414
422
-a, --authorization string Authentication token that must be sent as a Bearer token in the 'Authorization' header or as the 'authorization' URL query parameter
415
423
-b, --buffer uint Maximum number of requests to queue before refusing subsequent ones until the queue is freed (zero for infinite) (default 10)
424
+
-Y, --cache-request-body Whether to cache the HTTP request's body so the hook can return right away
416
425
-c, --concurrency uint Maximum number of times the command should be executed in parallel (zero for infinite concurrency) (default 1)
417
426
-C, --cwd string Working directory in which to run the command
418
427
-B, --forward-request-body Whether to forward each HTTP request's body to the the command's standard input
utils.Uint64Option(&buffer, "buffer", "b", "BUFFER", 10, "Maximum number of requests to queue before refusing subsequent ones until the queue is freed (zero for infinite)", errHandler)
68
72
utils.Uint64Option(&concurrency, "concurrency", "c", "CONCURRENCY", 1, "Maximum number of times the command should be executed in parallel (zero for infinite concurrency)", errHandler)
69
73
utils.BoolOption(&forwardRequestBody, "forward-request-body", "B", "FORWARD_REQUEST_BODY", false, "Whether to forward each HTTP request's body to the the command's standard input", errHandler)
74
+
utils.BoolOption(&cacheRequestBody, "cache-request-body", "Y", "CACHE_REQUEST_BODY", false, "Whether to cache the HTTP request's body so the hook can return right away", errHandler)
70
75
utils.BoolOption(&forwardRequestHeaders, "forward-request-headers", "H", "FORWARD_REQUEST_HEADERS", false, "Whether to forward each HTTP request's headers to the the command as environment variables (e.g. Content-Type becomes $MONOHOOK_REQUEST_HEADER_CONTENT_TYPE)", errHandler)
71
76
utils.BoolOption(&forwardRequestURL, "forward-request-url", "U", "FORWARD_REQUEST_URL", false, "Whether to forward each HTTP request's URL to the the command as the $MONOHOOK_REQUEST_URL environment variable", errHandler)
72
77
utils.StringOption(&cwd, "cwd", "C", "CWD", "", "Working directory in which to run the command")
@@ -80,7 +85,9 @@ func main() {
80
85
81
86
flag.Parse()
82
87
83
-
ifport>65535 {
88
+
ifcacheRequestBody&&!forwardRequestBody {
89
+
utils.Fail(1, quiet, "option -Y, --cache-request-body can only be used together with option -B, --forward-request-body")
90
+
} elseifport>65535 {
84
91
utils.Fail(1, quiet, "option -p, --port or environment variable $PORT must be an integer smaller than or equal to 65535")
85
92
}
86
93
@@ -140,9 +147,20 @@ func main() {
140
147
// Forward HTTP request body to the command's standard input.
141
148
varpw*io.PipeWriter
142
149
ifforwardRequestBody {
143
-
reader, writer:=io.Pipe()
144
-
opts.reader=reader
145
-
pw=writer
150
+
ifcacheRequestBody {
151
+
body, err:=ioutil.ReadAll(r.Body)
152
+
iferr!=nil {
153
+
utils.Print(quiet, "Warning: could not read request body: %s\n", err)
154
+
w.WriteHeader(400)
155
+
return
156
+
}
157
+
158
+
opts.cache=body
159
+
} else {
160
+
reader, writer:=io.Pipe()
161
+
opts.reader=reader
162
+
pw=writer
163
+
}
146
164
}
147
165
148
166
varenv []string
@@ -171,8 +189,16 @@ func main() {
171
189
}
172
190
173
191
ifpw!=nil {
174
-
io.Copy(pw, r.Body)
175
-
pw.Close()
192
+
193
+
_, err:=io.Copy(pw, r.Body)
194
+
iferr!=nil {
195
+
utils.Print(quiet, "Warning: could not read entire request body: %s\n", err)
196
+
}
197
+
198
+
pwErr:=pw.Close()
199
+
ifpwErr!=nil {
200
+
utils.Print(quiet, "Warning: could not close request body pipe: %s\n", pwErr)
0 commit comments