Skip to content

Commit df9bb20

Browse files
authored
Merge features pull request
features
2 parents 4dccb0f + 858b664 commit df9bb20

File tree

5 files changed

+76
-8
lines changed

5 files changed

+76
-8
lines changed

README.md

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@ Parameters:
1717

1818
For help type **erised -h**
1919

20-
Upon executing **erised** with no parameters it will listen on port **8080** for incoming http requests.
20+
Upon executing **erised** with no parameters it will listen on port **8080** for incoming http requests.
21+
22+
The latest version is available as a Docker image at [edaddario/erised](https://hub.docker.com/r/edaddario/erised)
23+
24+
```sh
25+
docker run --rm -p 8080:8080 edaddario/erised
26+
```
2127

2228
HTTP methods (e.g. GET, POST, PATCH, etc.), query strings and body are **ignored**. URL Paths are also ignored, except for:
2329

@@ -31,11 +37,12 @@ Response behaviour is controlled via custom headers in the http request:
3137

3238
|Name|Purpose|
3339
|--|--|
34-
|X-Erised-Data|Returns the **same** value in the response body|
3540
|X-Erised-Content-Type|Sets the response _Content-Type_. Valid values are **text** (default) for _text/plain_, **json** for _application/json_, **xml** for _application/xml_ and **gzip** for _application/octet-stream_. When using **gzip**, _Content-Encoding_ is also set to **gzip** and the response body is compressed accordingly.|
41+
|X-Erised-Data|Returns the **same** value in the response body|
42+
|X-Erised-Headers|Returns the value(s) in the response header. Values **must** be in a JSON array|
43+
|X-Erised-Location|Sets the response _Location_ to the new (redirected) URL or path, when 300 ≤ _X-Erised-Status-Code_ < 310|
44+
|X-Erised-Response-Delay|Number of **milliseconds** to wait before sending response back to client|
3645
|X-Erised-Status-Code|Sets the HTTP Status Code|
37-
|X-Erised-Location|Sets the response _Location_ to the new (redirected) URL or path, when 300 ≤ _X-Erised-Status-Code_ < 310
38-
|X-Erised-Response-Delay|Number of milliseconds to wait before sending response back to client
3946

4047
By design, no validation is performed on _X-Erised-Data_ or _X-Erised-Location_.
4148

@@ -77,6 +84,7 @@ NetworkAuthenticationRequired or 511
7784
Any other value will resolve to 200 (OK)
7885

7986
# Release History
87+
* v0.2.2 - Add custom headers, add dockerfile
8088
* v0.2.1 - Add gzip compression, improve erised/headers json handling
8189
* v0.0.3 - Add erised/headers, erised/ip and erised/info paths. Add delayed responses
8290
* v0.0.2 - Add HTTP redirection status codes (300's), startup configuration parameters and request's logging
@@ -89,7 +97,6 @@ Of all its deficiencies, the most notable are:
8997
* There are not tests (yet)
9098
* Server does not shutdown gracefully. To stop, process must be terminated
9199
* https protocol is not supported
92-
* **erised** does not scale well
93100

94101
I may or may not address any of this in a future release. Caveat Emptor
95102

@@ -210,6 +217,18 @@ curl -w '\n' -v http://localhost:8080
210217
* Closing connection 0
211218
```
212219
220+
### Simple request returning custom headers only:
221+
```
222+
curl -w '\n' -I -H "X-Erised-Headers:{\"My-Header\":\"Hello World\",\"Another-Header\":\"Goodbye World\"}" http://localhost:8080
223+
```
224+
```sh
225+
HTTP/1.1 200 OK
226+
Another-Header: Goodbye World
227+
Content-Encoding: identity
228+
Content-Type: text/plain
229+
My-Header: Hello World
230+
Date: Sat, 13 Mar 2021 22:56:09 GMT
231+
```
213232
### Request returning _Hello World_ in the response's body:
214233
```
215234
curl -w '\n' -v -H "X-Erised-Data:Hello World" http://localhost:8080

cmd/erised/.dockerignore

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Common
2+
README.md
3+
CHANGELOG.md
4+
docker-compose*.yml
5+
Dockerfile*
6+
7+
# git
8+
.git
9+
.gitattributes
10+
.gitignore
11+
12+
# JetBrains
13+
.idea
14+
15+
# Other
16+
bin/

cmd/erised/Dockerfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
FROM golang:alpine
2+
RUN apk update && \
3+
apk upgrade && \
4+
rm -rf /var/cache/apk/*
5+
WORKDIR /go/src/app
6+
COPY . .
7+
8+
RUN go build -o bin/erised -ldflags "-s -w"
9+
10+
EXPOSE 8080
11+
12+
CMD ["bin/erised"]

cmd/erised/main.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"time"
1010
)
1111

12-
const version = "v0.2.1"
12+
const version = "v0.2.2"
1313

1414
type server struct {
1515
mux *http.ServeMux
@@ -42,11 +42,12 @@ func setupFlags(f *flag.FlagSet) {
4242
fmt.Println("\nParameters:")
4343
flag.PrintDefaults()
4444
fmt.Println("\nHTTP Headers:")
45-
fmt.Println("X-Erised-Data:\t\t\tReturns the same value in the response body")
4645
fmt.Println("X-Erised-Content-Type:\t\tSets the response Content-Type")
47-
fmt.Println("X-Erised-Status-Code:\t\tSets the HTTP Status Code")
46+
fmt.Println("X-Erised-Data:\t\t\tReturns the same value in the response body")
47+
fmt.Println("X-Erised-Headers:\t\tReturns the value(s) in the response header(s). Values must be in a JSON array")
4848
fmt.Println("X-Erised-Location:\t\tSets the response Location when 300 ≤ X-Erised-Status-Code < 310")
4949
fmt.Println("X-Erised-Response-Delay:\tNumber of milliseconds to wait before sending response back to client")
50+
fmt.Println("X-Erised-Status-Code:\t\tSets the HTTP Status Code")
5051
fmt.Println()
5152
}
5253
}

cmd/erised/serverRoutes.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"log"
67
"net/http"
78
"strconv"
@@ -26,10 +27,27 @@ func (s *server) handleLanding() http.HandlerFunc {
2627

2728
res.Header().Set("Content-Type", ct)
2829
res.Header().Set("Content-Encoding", ce)
30+
2931
if rd, err := strconv.Atoi(req.Header.Get("X-Erised-Response-Delay")); err == nil {
3032
delay = time.Duration(rd) * time.Millisecond
3133
}
34+
35+
hd := req.Header.Get("X-Erised-Headers")
36+
37+
if json.Valid([]byte(hd)) {
38+
var rs map[string]interface{}
39+
_ = json.Unmarshal([]byte(hd), &rs)
40+
41+
if len(rs) != 0 {
42+
43+
for k, v := range rs {
44+
res.Header().Set(k, fmt.Sprintf("%v", v))
45+
}
46+
}
47+
}
48+
3249
sc := httpStatusCode(req.Header.Get("X-Erised-Status-Code"))
50+
3351
if sc >= 300 && sc < 310 {
3452
res.Header().Set("Location", req.Header.Get("X-Erised-Location"))
3553
}
@@ -50,6 +68,7 @@ func (s *server) handleHeaders() http.HandlerFunc {
5068
res.Header().Set("Content-Type", "application/json")
5169

5270
data := "{"
71+
5372
for k, v := range req.Header {
5473
if k == "X-Erised-Data" {
5574
if json.Valid([]byte(v[0])) {
@@ -61,6 +80,7 @@ func (s *server) handleHeaders() http.HandlerFunc {
6180
data += "\"" + k + "\":\"" + v[0] + "\","
6281
}
6382
}
83+
6484
data += "\"Host\":\"" + req.Host + "\""
6585
data += "}"
6686

0 commit comments

Comments
 (0)