From 2831af1040228c971897db447749408e5093bf4d Mon Sep 17 00:00:00 2001 From: helight Date: Tue, 29 Sep 2020 14:32:35 +0800 Subject: [PATCH 1/2] add httserver demo for test and add config.yaml Signed-off-by: helight --- http-filter-example/README.md | 32 +++++++++++++++++-- http-filter-example/config.yaml | 51 +++++++++++++++++++++++++++++++ http-filter-example/httpserver.py | 27 ++++++++++++++++ 3 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 http-filter-example/config.yaml create mode 100644 http-filter-example/httpserver.py diff --git a/http-filter-example/README.md b/http-filter-example/README.md index 46721b7a5..adfc02965 100644 --- a/http-filter-example/README.md +++ b/http-filter-example/README.md @@ -48,12 +48,40 @@ http_filters: - name: sample # before envoy.router because order matters! typed_config: "@type": type.googleapis.com/sample.Decoder - key: via + key: Via val: sample-filter - name: envoy.router typed_config: {} ``` - +or you can just use this demo yaml file [config.yaml](config.yaml). +- Run envoy with this cmd: +```sh +./bazel-bin/envoy --config-path ./http-filter-example/config.yaml -l trace +``` +- Run an http server +```sh +python httpserver.py +``` +- Access the http server with curl cmd: +```sh +curl http://127.0.0.1:8080 +``` +You can find there is an kv string in the request header to the http server, +and the key value `Via` has become to `via`, you can see the request header +in the http server stdout. +```sh +$ python httpserver.py +Started httpserver on port 8081 +127.0.0.1 - - [29/Sep/2020 11:30:03] "GET / HTTP/1.1" 200 - +host: 127.0.0.1:8080 +user-agent: curl/7.58.0 +accept: */* +x-forwarded-proto: http +x-request-id: b7a8d227-77a0-4985-a121-5e0f70bd16f2 +via: sample-filter +x-envoy-expected-rq-timeout-ms: 15000 +content-length: 0 +``` [StreamDecoderFilter]: https://github.com/envoyproxy/envoy/blob/b2610c84aeb1f75c804d67effcb40592d790e0f1/include/envoy/http/filter.h#L300 [StreamEncoderFilter]: https://github.com/envoyproxy/envoy/blob/b2610c84aeb1f75c804d67effcb40592d790e0f1/include/envoy/http/filter.h#L413 diff --git a/http-filter-example/config.yaml b/http-filter-example/config.yaml new file mode 100644 index 000000000..f75d6bc85 --- /dev/null +++ b/http-filter-example/config.yaml @@ -0,0 +1,51 @@ +admin: + access_log_path: /dev/null + address: + socket_address: + address: 127.0.0.1 + port_value: 0 +static_resources: + clusters: + name: cluster_0 + connect_timeout: 0.25s + load_assignment: + cluster_name: cluster_0 + endpoints: + - lb_endpoints: + - endpoint: + address: + socket_address: + address: 127.0.0.1 + port_value: 8081 + listeners: + - name: listener_0 + address: + socket_address: + address: 127.0.0.1 + port_value: 8080 + filter_chains: + - filters: + - name: envoy.http_connection_manager + typed_config: + "@type": type.googleapis.com/envoy.config.filter.network.http_connection_manager.v2.HttpConnectionManager + stat_prefix: ingress_http + codec_type: auto + route_config: + name: local_route + virtual_hosts: + - name: local_service + domains: + - "*" + routes: + - match: + prefix: "/" + route: + cluster: cluster_0 + http_filters: + - name: sample # before envoy.router because order matters! + typed_config: + "@type": type.googleapis.com/sample.Decoder + key: vIa + val: sample-filter + - name: envoy.router + typed_config: {} diff --git a/http-filter-example/httpserver.py b/http-filter-example/httpserver.py new file mode 100644 index 000000000..9f2894d25 --- /dev/null +++ b/http-filter-example/httpserver.py @@ -0,0 +1,27 @@ +#!/usr/bin/python +from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer + +PORT = 8081 + +class doHandler(BaseHTTPRequestHandler): + # Handler for the GET requests + def do_GET(self): + self.send_response(200) + self.send_header('Content-type','text/html') + self.end_headers() + print self.headers # print the http header + # Send the html message + self.wfile.write("Hello World !") + return + +try: + # Create a web server and define the handler to manage the incoming request + server = HTTPServer(('', PORT), doHandler) + print 'Started httpserver on port ' , PORT + + # Wait forever for incoming http requests + server.serve_forever() + +except KeyboardInterrupt: + print 'shutting down the web server' + server.socket.close() From 23a01a41303e53da76c38136015f65b1549ff412 Mon Sep 17 00:00:00 2001 From: helight Date: Wed, 30 Sep 2020 15:57:45 +0800 Subject: [PATCH 2/2] add verity script Signed-off-by: helight --- http-filter-example/README.md | 6 +++++- http-filter-example/httpserver.py | 23 +++++++++++--------- http-filter-example/start_service.sh | 3 +++ http-filter-example/verify.sh | 32 ++++++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 11 deletions(-) create mode 100644 http-filter-example/start_service.sh create mode 100644 http-filter-example/verify.sh diff --git a/http-filter-example/README.md b/http-filter-example/README.md index adfc02965..2c5d32693 100644 --- a/http-filter-example/README.md +++ b/http-filter-example/README.md @@ -60,7 +60,7 @@ or you can just use this demo yaml file [config.yaml](config.yaml). ``` - Run an http server ```sh -python httpserver.py +python ./http-filter-example/httpserver.py ``` - Access the http server with curl cmd: ```sh @@ -82,6 +82,10 @@ via: sample-filter x-envoy-expected-rq-timeout-ms: 15000 content-length: 0 ``` +- Start service and verify with shell script + +1. `bash ./http-filter-example/start_service.sh` +2. `bash ./http-filter-example/verify.sh` [StreamDecoderFilter]: https://github.com/envoyproxy/envoy/blob/b2610c84aeb1f75c804d67effcb40592d790e0f1/include/envoy/http/filter.h#L300 [StreamEncoderFilter]: https://github.com/envoyproxy/envoy/blob/b2610c84aeb1f75c804d67effcb40592d790e0f1/include/envoy/http/filter.h#L413 diff --git a/http-filter-example/httpserver.py b/http-filter-example/httpserver.py index 9f2894d25..c60f99ca6 100644 --- a/http-filter-example/httpserver.py +++ b/http-filter-example/httpserver.py @@ -1,27 +1,30 @@ -#!/usr/bin/python -from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer +#!/usr/bin/python3 +from http.server import BaseHTTPRequestHandler,HTTPServer -PORT = 8081 +HTTP_PORT = 8081 +# This class will handles any incoming request class doHandler(BaseHTTPRequestHandler): + # Handler for the GET requests def do_GET(self): self.send_response(200) self.send_header('Content-type','text/html') self.end_headers() - print self.headers # print the http header - # Send the html message - self.wfile.write("Hello World !") + print(self.headers) + # Send the message + self.wfile.write(b"Hello World !\r\n") + self.wfile.write(self.headers.as_bytes()) return try: - # Create a web server and define the handler to manage the incoming request - server = HTTPServer(('', PORT), doHandler) - print 'Started httpserver on port ' , PORT + # Create a http server and define the handler to manage the request + server = HTTPServer(('', HTTP_PORT), doHandler) + print('Started httpserver on port ' , HTTP_PORT) # Wait forever for incoming http requests server.serve_forever() except KeyboardInterrupt: - print 'shutting down the web server' + print('^C received, shutting down the web server') server.socket.close() diff --git a/http-filter-example/start_service.sh b/http-filter-example/start_service.sh new file mode 100644 index 000000000..ae30c9e66 --- /dev/null +++ b/http-filter-example/start_service.sh @@ -0,0 +1,3 @@ +#!/bin/sh +python3 ./http-filter-example/httpserver.py& +./bazel-bin/envoy --config-path ./http-filter-example/config.yaml diff --git a/http-filter-example/verify.sh b/http-filter-example/verify.sh new file mode 100644 index 000000000..f2d345f13 --- /dev/null +++ b/http-filter-example/verify.sh @@ -0,0 +1,32 @@ +#!/bin/bash -e + +export NAME=http-filter-example + +_curl () { + local arg curl_command + curl_command=(curl -s) + if [[ ! "$*" =~ "-X" ]]; then + curl_command+=(-X GET) + fi + for arg in "${@}"; do + curl_command+=("$arg") + done + "${curl_command[@]}" || { + echo "ERROR: curl (${curl_command[*]})" >&2 + return 1 + } +} + +responds_with () { + local expected + expected="$1" + shift + _curl "${@}" | grep "$expected" || { + echo "ERROR: curl expected (${*}): $expected" >&2 + return 1 + } +} + +responds_with \ + "via: sample-filter" \ + "http://localhost:8080"