Skip to content

Commit

Permalink
Merge features pull request
Browse files Browse the repository at this point in the history
features
  • Loading branch information
EAddario authored Mar 14, 2021
2 parents 4dccb0f + 858b664 commit df9bb20
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 8 deletions.
29 changes: 24 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ Parameters:

For help type **erised -h**

Upon executing **erised** with no parameters it will listen on port **8080** for incoming http requests.
Upon executing **erised** with no parameters it will listen on port **8080** for incoming http requests.

The latest version is available as a Docker image at [edaddario/erised](https://hub.docker.com/r/edaddario/erised)

```sh
docker run --rm -p 8080:8080 edaddario/erised
```

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

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

|Name|Purpose|
|--|--|
|X-Erised-Data|Returns the **same** value in the response body|
|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.|
|X-Erised-Data|Returns the **same** value in the response body|
|X-Erised-Headers|Returns the value(s) in the response header. Values **must** be in a JSON array|
|X-Erised-Location|Sets the response _Location_ to the new (redirected) URL or path, when 300 ≤ _X-Erised-Status-Code_ < 310|
|X-Erised-Response-Delay|Number of **milliseconds** to wait before sending response back to client|
|X-Erised-Status-Code|Sets the HTTP Status Code|
|X-Erised-Location|Sets the response _Location_ to the new (redirected) URL or path, when 300 ≤ _X-Erised-Status-Code_ < 310
|X-Erised-Response-Delay|Number of milliseconds to wait before sending response back to client

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

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

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

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

Expand Down Expand Up @@ -210,6 +217,18 @@ curl -w '\n' -v http://localhost:8080
* Closing connection 0
```
### Simple request returning custom headers only:
```
curl -w '\n' -I -H "X-Erised-Headers:{\"My-Header\":\"Hello World\",\"Another-Header\":\"Goodbye World\"}" http://localhost:8080
```
```sh
HTTP/1.1 200 OK
Another-Header: Goodbye World
Content-Encoding: identity
Content-Type: text/plain
My-Header: Hello World
Date: Sat, 13 Mar 2021 22:56:09 GMT
```
### Request returning _Hello World_ in the response's body:
```
curl -w '\n' -v -H "X-Erised-Data:Hello World" http://localhost:8080
Expand Down
16 changes: 16 additions & 0 deletions cmd/erised/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Common
README.md
CHANGELOG.md
docker-compose*.yml
Dockerfile*

# git
.git
.gitattributes
.gitignore

# JetBrains
.idea

# Other
bin/
12 changes: 12 additions & 0 deletions cmd/erised/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM golang:alpine
RUN apk update && \
apk upgrade && \
rm -rf /var/cache/apk/*
WORKDIR /go/src/app
COPY . .

RUN go build -o bin/erised -ldflags "-s -w"

EXPOSE 8080

CMD ["bin/erised"]
7 changes: 4 additions & 3 deletions cmd/erised/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"time"
)

const version = "v0.2.1"
const version = "v0.2.2"

type server struct {
mux *http.ServeMux
Expand Down Expand Up @@ -42,11 +42,12 @@ func setupFlags(f *flag.FlagSet) {
fmt.Println("\nParameters:")
flag.PrintDefaults()
fmt.Println("\nHTTP Headers:")
fmt.Println("X-Erised-Data:\t\t\tReturns the same value in the response body")
fmt.Println("X-Erised-Content-Type:\t\tSets the response Content-Type")
fmt.Println("X-Erised-Status-Code:\t\tSets the HTTP Status Code")
fmt.Println("X-Erised-Data:\t\t\tReturns the same value in the response body")
fmt.Println("X-Erised-Headers:\t\tReturns the value(s) in the response header(s). Values must be in a JSON array")
fmt.Println("X-Erised-Location:\t\tSets the response Location when 300 ≤ X-Erised-Status-Code < 310")
fmt.Println("X-Erised-Response-Delay:\tNumber of milliseconds to wait before sending response back to client")
fmt.Println("X-Erised-Status-Code:\t\tSets the HTTP Status Code")
fmt.Println()
}
}
Expand Down
20 changes: 20 additions & 0 deletions cmd/erised/serverRoutes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"encoding/json"
"fmt"
"log"
"net/http"
"strconv"
Expand All @@ -26,10 +27,27 @@ func (s *server) handleLanding() http.HandlerFunc {

res.Header().Set("Content-Type", ct)
res.Header().Set("Content-Encoding", ce)

if rd, err := strconv.Atoi(req.Header.Get("X-Erised-Response-Delay")); err == nil {
delay = time.Duration(rd) * time.Millisecond
}

hd := req.Header.Get("X-Erised-Headers")

if json.Valid([]byte(hd)) {
var rs map[string]interface{}
_ = json.Unmarshal([]byte(hd), &rs)

if len(rs) != 0 {

for k, v := range rs {
res.Header().Set(k, fmt.Sprintf("%v", v))
}
}
}

sc := httpStatusCode(req.Header.Get("X-Erised-Status-Code"))

if sc >= 300 && sc < 310 {
res.Header().Set("Location", req.Header.Get("X-Erised-Location"))
}
Expand All @@ -50,6 +68,7 @@ func (s *server) handleHeaders() http.HandlerFunc {
res.Header().Set("Content-Type", "application/json")

data := "{"

for k, v := range req.Header {
if k == "X-Erised-Data" {
if json.Valid([]byte(v[0])) {
Expand All @@ -61,6 +80,7 @@ func (s *server) handleHeaders() http.HandlerFunc {
data += "\"" + k + "\":\"" + v[0] + "\","
}
}

data += "\"Host\":\"" + req.Host + "\""
data += "}"

Expand Down

0 comments on commit df9bb20

Please sign in to comment.