hewei.it 4 năm trước cách đây
mục cha
commit
7280b6d760

+ 1 - 1
etc/httpd.conf

@@ -12,7 +12,7 @@ worker_processes = auto
 # http server
 http_port = 8080
 https_port = 8443
-#base_url = /v1/api
+#base_url = /api/v1
 document_root = html
 home_page = index.html
 #error_page = error.html

+ 37 - 0
etc/nginx.conf

@@ -0,0 +1,37 @@
+# cd libhv
+# sudo nginx -p . -c etc/nginx.conf
+# bin/httpd -c etc/httpd.conf -s restart -d
+# bin/curl -v http://127.0.0.1/api/v1/get
+
+worker_processes    auto;
+
+pid         logs/nginx.pid;
+error_log   logs/error.log;
+
+events {
+    worker_connections  1024;
+}
+
+http {
+    access_log      logs/access.log;
+
+    server {
+        listen      80;
+
+        # static files service
+        location / {
+            root    html;
+            index   index.html;
+        }
+
+        # autoindex service
+        location /downloads/ {
+            autoindex   on;
+        }
+
+        # api service: nginx => libhv
+        location /api/v1/ {
+            proxy_pass  http://127.0.0.1:8080/;
+        }
+    }
+}

+ 1 - 1
examples/httpd/handler.h

@@ -196,7 +196,7 @@ public:
         if (req->content_type != MULTIPART_FORM_DATA) {
             return response_status(resp, HTTP_STATUS_BAD_REQUEST);
         }
-        FormData file = req->form["file"];
+        const FormData& file = req->form["file"];
         if (file.content.empty()) {
             return response_status(resp, HTTP_STATUS_BAD_REQUEST);
         }

+ 1 - 1
http/Http1Parser.cpp

@@ -114,7 +114,7 @@ int on_headers_complete(http_parser* parser) {
     if (iter != hp->parsed->headers.end()) {
         int content_length = atoi(iter->second.c_str());
         hp->parsed->content_length = content_length;
-        int reserve_length = MIN(content_length, MAX_CONTENT_LENGTH);
+        int reserve_length = MIN(content_length + 1, MAX_CONTENT_LENGTH);
         if ((!skip_body) && reserve_length > hp->parsed->body.capacity()) {
             hp->parsed->body.reserve(reserve_length);
         }

+ 1 - 1
http/HttpMessage.h

@@ -149,7 +149,7 @@ public:
         if (content_type != MULTIPART_FORM_DATA) {
             return HTTP_STATUS_BAD_REQUEST;
         }
-        FormData formdata = form[name];
+        const FormData& formdata = form[name];
         if (formdata.content.empty()) {
             return HTTP_STATUS_BAD_REQUEST;
         }

+ 1 - 1
http/WebSocketParser.cpp

@@ -13,7 +13,7 @@ static int on_frame_header(websocket_parser* parser) {
         wp->opcode = opcode;
     }
     int length = parser->length;
-    int reserve_length = MIN(length, MAX_PAYLOAD_LENGTH);
+    int reserve_length = MIN(length + 1, MAX_PAYLOAD_LENGTH);
     if (reserve_length > wp->message.capacity()) {
         wp->message.reserve(reserve_length);
     }

+ 3 - 3
http/http_content.cpp

@@ -4,7 +4,7 @@
 
 #include <string.h>
 
-std::string dump_query_params(QueryParams& query_params) {
+std::string dump_query_params(const QueryParams& query_params) {
     std::string query_string;
     for (auto& pair : query_params) {
         if (query_string.size() != 0) {
@@ -209,7 +209,7 @@ static int on_body_end(multipart_parser* parser) {
     userdata->state = MP_BODY_END;
     return 0;
 }
-int parse_multipart(std::string& str, MultiPart& mp, const char* boundary) {
+int parse_multipart(const std::string& str, MultiPart& mp, const char* boundary) {
     //printf("boundary=%s\n", boundary);
     std::string __boundary("--");
     __boundary += boundary;
@@ -231,7 +231,7 @@ int parse_multipart(std::string& str, MultiPart& mp, const char* boundary) {
     return nparse == str.size() ? 0 : -1;
 }
 
-std::string dump_json(hv::Json& json, int indent) {
+std::string dump_json(const hv::Json& json, int indent) {
     return json.dump(indent);
 }
 

+ 3 - 3
http/http_content.h

@@ -6,7 +6,7 @@
 
 // QueryParams
 typedef hv::KeyValue    QueryParams;
-HV_EXPORT std::string dump_query_params(QueryParams& query_params);
+HV_EXPORT std::string dump_query_params(const QueryParams& query_params);
 HV_EXPORT int         parse_query_params(const char* query_string, QueryParams& query_params);
 
 // NOTE: WITHOUT_HTTP_CONTENT
@@ -49,7 +49,7 @@ struct FormData {
 typedef HV_MAP<std::string, FormData>          MultiPart;
 #define DEFAULT_MULTIPART_BOUNDARY  "----WebKitFormBoundary7MA4YWxkTrZu0gW"
 HV_EXPORT std::string dump_multipart(MultiPart& mp, const char* boundary = DEFAULT_MULTIPART_BOUNDARY);
-HV_EXPORT int         parse_multipart(std::string& str, MultiPart& mp, const char* boundary);
+HV_EXPORT int         parse_multipart(const std::string& str, MultiPart& mp, const char* boundary);
 
 // Json
 // https://github.com/nlohmann/json
@@ -59,7 +59,7 @@ using Json = nlohmann::json;
 // using Json = nlohmann::ordered_json;
 }
 
-HV_EXPORT std::string dump_json(hv::Json& json, int indent = -1);
+HV_EXPORT std::string dump_json(const hv::Json& json, int indent = -1);
 HV_EXPORT int         parse_json(const char* str, hv::Json& json, std::string& errmsg);
 #endif
 

+ 1 - 1
http/server/HttpServer.h

@@ -44,7 +44,7 @@ typedef struct http_server_s {
 
 int main() {
     HttpService service;
-    service.base_url = "/v1/api";
+    service.base_url = "/api/v1";
     service.GET("/ping", [](HttpRequest* req, HttpResponse* resp) {
         resp->body = "pong";
         return 200;

+ 1 - 1
http/server/HttpService.h

@@ -11,7 +11,7 @@
 #include "HttpMessage.h"
 #include "HttpResponseWriter.h"
 
-#define DEFAULT_BASE_URL        "/v1/api"
+#define DEFAULT_BASE_URL        "/api/v1"
 #define DEFAULT_DOCUMENT_ROOT   "/var/www/html"
 #define DEFAULT_HOME_PAGE       "index.html"
 #define DEFAULT_ERROR_PAGE      "error.html"