Skip to content

Commit b10e3f6

Browse files
authored
Merge pull request #69 from mendhak/issue-67-header-case
Preserve header case. File contents to override response.
2 parents cd8125c + bb848e8 commit b10e3f6

File tree

6 files changed

+102
-16
lines changed

6 files changed

+102
-16
lines changed

.github/workflows/build.yml

+17-13
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,17 @@ jobs:
4545
images: |
4646
mendhak/http-https-echo
4747
48-
- name: Build the image multi-platform
49-
uses: docker/build-push-action@v5
50-
with:
51-
context: .
52-
platforms: linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64/v8,linux/ppc64le
53-
push: false
54-
cache-from: type=gha
55-
cache-to: type=gha,mode=max
56-
tags: ${{ steps.meta.outputs.tags }}
57-
labels: ${{ steps.meta.outputs.labels }}
48+
# Commenting out, possible bug: https://github.com/nodejs/docker-node/issues/1946
49+
# - name: Build the image multi-platform
50+
# uses: docker/build-push-action@v5
51+
# with:
52+
# context: .
53+
# platforms: linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64/v8,linux/ppc64le
54+
# push: false
55+
# cache-from: type=gha
56+
# cache-to: type=gha,mode=max
57+
# tags: ${{ steps.meta.outputs.tags }}
58+
# labels: ${{ steps.meta.outputs.labels }}
5859

5960
# Due to bug https://github.com/docker/buildx/issues/59, need to build for single platform, load, then run tests.
6061
- name: Build a test image single platform and load it
@@ -65,7 +66,9 @@ jobs:
6566
load: true
6667
cache-from: type=gha
6768
cache-to: type=gha,mode=max
68-
tags: "mendhak/http-https-echo:testing"
69+
tags: |
70+
${{ steps.meta.outputs.tags }}
71+
"mendhak/http-https-echo:testing"
6972
labels: ${{ steps.meta.outputs.labels }}
7073

7174
- name: Run tests using the test image
@@ -75,9 +78,10 @@ jobs:
7578
id: scan
7679
uses: anchore/scan-action@v3
7780
with:
78-
image: "mendhak/http-https-echo:latest"
81+
image: "mendhak/http-https-echo:testing"
7982
output-format: sarif
80-
severity-cutoff: critical
83+
# severity-cutoff: critical
84+
fail-build: false
8185

8286
- name: upload Anchore scan SARIF report
8387
uses: github/codeql-action/upload-sarif@v3

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
2+
## Version `34` - `2024-08-11`
3+
* Set `PRESERVE_HEADER_CASE` to `1` to attempt to preserve the case of headers in the response.
4+
15
## Version `33` - `2024-04-07`
26
* Implementing configurable CORS settings by [ash0ne](https://github.com/mendhak/docker-http-https-echo/pull/65).
37

Dockerfile

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM node:16-alpine AS build
1+
FROM node:18-alpine AS build
22

33
WORKDIR /app
44
COPY . /app
@@ -19,7 +19,7 @@ RUN set -ex \
1919
&& chown -R node:node /app \
2020
&& chmod +r /app/privkey.pem
2121

22-
FROM node:16-alpine AS final
22+
FROM node:18-alpine AS final
2323
LABEL \
2424
org.opencontainers.image.title="http-https-echo" \
2525
org.opencontainers.image.description="Docker image that echoes request data as JSON; listens on HTTP/S, with various extra features, useful for debugging." \

README.md

+20
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ This image is executed as non root by default and is fully compliant with Kubern
3232
- [Include environment variables in the response](#include-environment-variables-in-the-response)
3333
- [Configuring CORS policy](#setting-corscross-origin-resource-sharing-headers-in-the-response)
3434
- [Client certificate details (mTLS) in the response](#client-certificate-details-mtls-in-the-response)
35+
- [Preserve the case of headers in response body](#preserve-the-case-of-headers-in-response-body)
36+
- [Override the response body with a file](#override-the-response-body-with-a-file)
3537
- [Prometheus Metrics](#prometheus-metrics)
3638
- [Screenshots](#screenshots)
3739
- [Building](#building)
@@ -278,6 +280,24 @@ If you browse to https://localhost:8443/ in Firefox, you won't get prompted to s
278280
openssl pkcs12 -export -in cert.pem -inkey privkey.pem -out certpkcs12.pfx
279281
```
280282

283+
## Preserve the case of headers in response body
284+
285+
By default, the headers in the response body are lowercased. To attempt to preserve the case of headers in the response body, set the environment variable `PRESERVE_HEADER_CASE` to true.
286+
287+
```bash
288+
docker run -e PRESERVE_HEADER_CASE=true -p 8080:8080 -p 8443:8443 --rm -t mendhak/http-https-echo:33
289+
```
290+
291+
## Override the response body with a file
292+
293+
To override the response body with a file, set the environment variable `OVERRIDE_RESPONSE_BODY_FILE_PATH` to a file path.
294+
The file path needs to be in the `/app` directory.
295+
296+
```bash
297+
docker run -d --rm -v ${PWD}/test.html:/app/test.html -p 8080:8080 -e OVERRIDE_RESPONSE_BODY_FILE_PATH=/test.html -t mendhak/http-https-echo:33
298+
```
299+
300+
281301
## Prometheus Metrics
282302

283303
To expose http performance metrics, set the `PROMETHEUS_ENABLED` environment variable to true, the metrics will be available at `/metrics`. This uses the [`express-prom-bundle`](https://github.com/jochen-schweizer/express-prom-bundle) middleware

index.js

+23
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ app.use(function(req, res, next){
4747

4848
//Handle all paths
4949
app.all('*', (req, res) => {
50+
51+
if(process.env.OVERRIDE_RESPONSE_BODY_FILE_PATH){
52+
// Path is relative to current directory
53+
res.sendFile(process.env.OVERRIDE_RESPONSE_BODY_FILE_PATH, { root : __dirname});
54+
return;
55+
}
56+
5057
const echo = {
5158
path: req.path,
5259
headers: req.headers,
@@ -69,6 +76,22 @@ app.all('*', (req, res) => {
6976
}
7077
};
7178

79+
if(process.env.PRESERVE_HEADER_CASE){
80+
let newHeaders = {...req.headers};
81+
82+
// req.headers is in lowercase, processed, deduplicated. req.rawHeaders is not.
83+
// Match on the preserved case of the header name, populate newHeaders with preserved case and processed value.
84+
for (let i = 0; i < req.rawHeaders.length; i += 2) {
85+
let preservedHeaderName = req.rawHeaders[i];
86+
if (preservedHeaderName == preservedHeaderName.toLowerCase()) { continue; }
87+
88+
newHeaders[preservedHeaderName] = req.header(preservedHeaderName);
89+
delete newHeaders[preservedHeaderName.toLowerCase()];
90+
}
91+
echo.headers = newHeaders;
92+
}
93+
94+
7295
//Add client certificate details to the output, if present
7396
//This only works if `requestCert` is true when starting the server.
7497
if(req.socket.getPeerCertificate){

tests.sh

+36-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ docker ps -aq --filter "name=http-echo-tests" | grep -q . && docker stop http-ec
4848

4949
message " Start container normally "
5050
docker run -d --rm --name http-echo-tests -p 8080:8080 -p 8443:8443 -t mendhak/http-https-echo:testing
51-
sleep 5
51+
sleep 10
5252

5353

5454
message " Make http(s) request, and test the path, method, header and status code. "
@@ -510,6 +510,41 @@ else
510510
exit 1
511511
fi
512512

513+
514+
message " Stop containers "
515+
docker stop http-echo-tests
516+
sleep 5
517+
518+
message " Start container with PRESERVE_HEADER_CASE enabled "
519+
docker run -d -e PRESERVE_HEADER_CASE=true --rm --name http-echo-tests -p 8080:8080 -p 8443:8443 -t mendhak/http-https-echo:testing
520+
521+
sleep 5
522+
HEADER_CASE_CHECK=$(curl -s -H "prEseRVe-CaSE: A1b2C3" -H 'x-a-b: 999' -H 'X-a-B: 13' localhost:8080 | jq -r '.headers."prEseRVe-CaSE"')
523+
if [[ "$HEADER_CASE_CHECK" == "A1b2C3" ]]
524+
then
525+
passed "PRESERVE_HEADER_CASE enabled"
526+
else
527+
failed "PRESERVE_HEADER_CASE failed"
528+
exit 1
529+
fi
530+
531+
message " Stop containers "
532+
docker stop http-echo-tests
533+
sleep 5
534+
535+
message " Start container with a custom response body from a file "
536+
echo "<h1>Hello World</h1>" > test.html
537+
docker run -d --rm -v ${PWD}/test.html:/app/test.html --name http-echo-tests -p 8080:8080 -e OVERRIDE_RESPONSE_BODY_FILE_PATH=/test.html -t mendhak/http-https-echo:testing
538+
sleep 5
539+
RESPONSE_BODY=$(curl -s http://localhost:8080)
540+
if [[ "$RESPONSE_BODY" == "<h1>Hello World</h1>" ]]
541+
then
542+
passed "Custom response body from file"
543+
else
544+
failed "Custom response body from file failed"
545+
exit 1
546+
fi
547+
513548
message " Stop containers "
514549
docker stop http-echo-tests
515550
sleep 5

0 commit comments

Comments
 (0)