فهرست منبع

HttpRequest::Host() Path()

hewei.it 4 سال پیش
والد
کامیت
fec0c52b1b
5فایلهای تغییر یافته به همراه33 افزوده شده و 13 حذف شده
  1. 1 1
      http/HttpMessage.cpp
  2. 23 6
      http/HttpMessage.h
  3. 5 6
      http/server/HttpHandler.cpp
  4. 2 0
      http/server/HttpHandler.h
  5. 2 0
      http/server/HttpServer.cpp

+ 1 - 1
http/HttpMessage.cpp

@@ -438,7 +438,7 @@ void HttpRequest::DumpUrl() {
         if (port == 0 ||
             port == DEFAULT_HTTP_PORT ||
             port == DEFAULT_HTTPS_PORT) {
-            str += host;
+            str += Host();
         }
         else {
             snprintf(c_str, sizeof(c_str), "%s:%d", host.c_str(), port);

+ 23 - 6
http/HttpMessage.h

@@ -117,6 +117,8 @@ public:
     }
 
     /*
+     * @usage   https://github.com/nlohmann/json
+     *
      * null:    Json(nullptr);
      * boolean: Json(true);
      * number:  Json(123);
@@ -127,7 +129,7 @@ public:
                     {"k2", "v2"}
                 }));
      * array:   Json(std::vector<ValueType>);
-                Json(hv::Json::object(
+                Json(hv::Json::array(
                     {1, 2, 3}
                 ));
      */
@@ -304,6 +306,26 @@ public:
 
     virtual std::string Dump(bool is_dump_headers, bool is_dump_body);
 
+    // structed url -> url
+    void DumpUrl();
+    // url -> structed url
+    void ParseUrl();
+
+    std::string Host() {
+        auto iter = headers.find("Host");
+        if (iter != headers.end()) {
+            host = iter->second;
+        }
+        return host;
+    }
+
+    std::string Path() {
+        const char* s = path.c_str();
+        const char* e = s;
+        while (*e && *e != '?' && *e != '#') ++e;
+        return std::string(s, e);
+    }
+
     std::string GetParam(const char* key, const std::string& defvalue = "") {
         auto iter = query_params.find(key);
         if (iter != query_params.end()) {
@@ -312,11 +334,6 @@ public:
         return defvalue;
     }
 
-    // structed url -> url
-    void DumpUrl();
-    // url -> structed url
-    void ParseUrl();
-
     // Range: bytes=0-4095
     void SetRange(long from = 0, long to = -1) {
         headers["Range"] = asprintf("bytes=%ld-%ld", from, to);

+ 5 - 6
http/server/HttpHandler.cpp

@@ -6,16 +6,18 @@
 int HttpHandler::HandleHttpRequest() {
     // preprocessor -> api -> web -> postprocessor
 
-    int status_code = 200;
+    int status_code = HTTP_STATUS_OK;
     http_sync_handler sync_handler = NULL;
     http_async_handler async_handler = NULL;
 
     HttpRequest* pReq = req.get();
     HttpResponse* pResp = resp.get();
 
-    pReq->ParseUrl();
+    pReq->scheme = ssl ? "https" : "http";
     pReq->client_addr.ip = ip;
     pReq->client_addr.port = port;
+    pReq->Host();
+    pReq->ParseUrl();
 
 preprocessor:
     state = HANDLE_BEGIN;
@@ -46,10 +48,7 @@ preprocessor:
             (pReq->method == HTTP_GET || pReq->method == HTTP_HEAD)) {
         // file service
         status_code = 200;
-        const char* s = pReq->path.c_str();
-        const char* e = s;
-        while (*e && *e != '?' && *e != '#') ++e;
-        std::string path = std::string(s, e);
+        std::string path = pReq->Path();
         const char* req_path = path.c_str();
         // path safe check
         if (*req_path != '/' || strstr(req_path, "/../")) {

+ 2 - 0
http/server/HttpHandler.h

@@ -65,6 +65,7 @@ public:
     } state;
 
     // peeraddr
+    bool                    ssl;
     char                    ip[64];
     int                     port;
 
@@ -89,6 +90,7 @@ public:
     HttpHandler() {
         protocol = UNKNOWN;
         state = WANT_RECV;
+        ssl = false;
         service = NULL;
         files = NULL;
         ws_cbs = NULL;

+ 2 - 0
http/server/HttpServer.cpp

@@ -260,6 +260,8 @@ static void on_accept(hio_t* io) {
     hio_set_keepalive_timeout(io, HIO_DEFAULT_KEEPALIVE_TIMEOUT);
     // new HttpHandler, delete on_close
     HttpHandler* handler = new HttpHandler;
+    // ssl
+    handler->ssl = hio_type(io) == HIO_TYPE_SSL;
     // ip
     sockaddr_ip((sockaddr_u*)hio_peeraddr(io), handler->ip, sizeof(handler->ip));
     // port