Browse Source

Merge branch 'dev'

hewei 6 năm trước cách đây
mục cha
commit
631659d02f
6 tập tin đã thay đổi với 42 bổ sung18 xóa
  1. 23 14
      README.md
  2. 1 0
      examples/http_api_test.h
  3. 3 3
      http/Http2Session.cpp
  4. 2 1
      http/Http2Session.h
  5. 11 0
      http/HttpMessage.h
  6. 2 0
      http/server/HttpHandler.cpp

+ 23 - 14
README.md

@@ -140,15 +140,16 @@ bin/nc -u 127.0.0.1 2222
 - sudo make install
 
 ### examples
-- make test # master-workers model
-- make timer # timer add/del/reset
-- make loop # event-loop(include idle, timer, io)
-- make tcp  # tcp server
-- make udp  # udp server
-- make nc   # network client
-- make nmap # host discovery
-- make httpd # http server
-- make curl # http client
+- make examples
+    - make test # master-workers model
+    - make timer # timer add/del/reset
+    - make loop # event-loop(include idle, timer, io)
+    - make tcp  # tcp server
+    - make udp  # udp server
+    - make nc   # network client
+    - make nmap # host discovery
+    - make httpd # http server
+    - make curl # http client
 
 ### unittest
 - make unittest
@@ -158,12 +159,14 @@ bin/nc -u 127.0.0.1 2222
 - make DEFINES=PRINT_DEBUG
 
 #### compile WITH_OPENSSL
-libhv combines OpenSSL perfectly, something almost all asynchronous IO network libraries don't do.
+libhv combines OpenSSL perfectly, something almost all asynchronous IO network libraries don't do.<br>
 And enable SSL in libhv is so easy, just only two apis:
 ```
-int ssl_ctx_init(const char* crt_file, const char* key_file, const char* ca_file); // init global SSL_CTX, see
-base/ssl_ctx.h
-int hio_enable_ssl(hio_t* io); // enable ssl, see event/hloop.h
+// init global SSL_CTX, see base/ssl_ctx.h
+int ssl_ctx_init(const char* crt_file, const char* key_file, const char* ca_file);
+
+// enable ssl, see event/hloop.h
+int hio_enable_ssl(hio_t* io);
 ```
 
 https is the best example.
@@ -181,7 +184,13 @@ curl -v https://localhost:8080 --insecure
 - make DEFINES="WITH_CURL CURL_STATICLIB"
 
 #### compile WITH_NGHTTP2
-- make DEFINES=WITH_NGHTTP2
+```
+sudo apt-get install libnghttp2-dev # ubuntu
+make clean
+make libhv httpd curl DEFINES=WITH_NGHTTP2
+bin/httpd -d
+bin/curl -v localhost:8080 --http2
+```
 
 #### other options
 - ENABLE_IPV6

+ 1 - 0
examples/http_api_test.h

@@ -16,6 +16,7 @@
 
 
 inline int http_api_preprocessor(HttpRequest* req, HttpResponse* res) {
+    //printf("%s:%d\n", req->client_addr.ip.c_str(), req->client_addr.port);
     //printf("%s\n", req->Dump(true, true).c_str());
     req->ParseBody();
     return 0;

+ 3 - 3
http/Http2Session.cpp

@@ -51,14 +51,14 @@ Http2Session::Http2Session(http_session_type type) {
         nghttp2_session_callbacks_set_on_frame_recv_callback(cbs, on_frame_recv_callback);
     }
     if (type == HTTP_CLIENT) {
-        nghttp2_session_client_new(&session, cbs, NULL);
+        nghttp2_session_client_new(&session, cbs, this);
         state = HSS_SEND_MAGIC;
     }
     else if (type == HTTP_SERVER) {
-        nghttp2_session_server_new(&session, cbs, NULL);
+        nghttp2_session_server_new(&session, cbs, this);
         state = HSS_WANT_RECV;
     }
-    nghttp2_session_set_user_data(session, this);
+    //nghttp2_session_set_user_data(session, this);
     submited = NULL;
     parsed = NULL;
     stream_id = -1;

+ 2 - 1
http/Http2Session.h

@@ -66,7 +66,8 @@ public:
     }
 
     virtual const char* StrError(int error) {
-        return nghttp2_http2_strerror(error);
+        //return nghttp2_http2_strerror(error);
+        return nghttp2_strerror(error);
     }
 
     // client

+ 11 - 0
http/HttpMessage.h

@@ -11,6 +11,15 @@
 typedef std::map<std::string, std::string, StringCaseLess>  http_headers;
 typedef std::string                                         http_body;
 
+struct NetAddr {
+    std::string     ip;
+    int             port;
+
+    std::string ipport() {
+        return asprintf("%s:%d", ip.c_str(), port);
+    }
+};
+
 class HttpMessage {
 public:
     int                 type;
@@ -105,6 +114,8 @@ public:
     int                 port;
     std::string         path;
     QueryParams         query_params;
+    // client_addr
+    NetAddr             client_addr;
 
     HttpRequest() : HttpMessage() {
         type = HTTP_REQUEST;

+ 2 - 0
http/server/HttpHandler.cpp

@@ -8,6 +8,8 @@ int HttpHandler::HandleRequest() {
     // preprocessor -> api -> web -> postprocessor
     // preprocessor
     req.ParseUrl();
+    req.client_addr.ip = ip;
+    req.client_addr.port = port;
     if (service->preprocessor) {
         if (service->preprocessor(&req, &res) == HANDLE_DONE) {
             return HANDLE_DONE;