1
0
ithewei 6 жил өмнө
parent
commit
7b0ae75f89

+ 12 - 1
http/HttpPayload.cpp

@@ -11,6 +11,7 @@ void HttpPayload::FillContentType() {
         goto append;
     }
 
+#ifndef WITHOUT_HTTP_CONTENT
     if (content_type == CONTENT_TYPE_NONE) {
         if (json.size() != 0) {
             content_type = APPLICATION_JSON;
@@ -25,11 +26,14 @@ void HttpPayload::FillContentType() {
             content_type = TEXT_PLAIN;
         }
     }
+#endif
 
     if (content_type != CONTENT_TYPE_NONE) {
         headers["Content-Type"] = http_content_type_str(content_type);
     }
+
 append:
+#ifndef WITHOUT_HTTP_CONTENT
     if (content_type == MULTIPART_FORM_DATA) {
         auto iter = headers.find("Content-Type");
         if (iter != headers.end()) {
@@ -41,6 +45,8 @@ append:
             }
         }
     }
+#endif
+    return;
 }
 
 void HttpPayload::FillContentLength() {
@@ -49,7 +55,7 @@ void HttpPayload::FillContentLength() {
         if (content_length == 0) {
             content_length = body.size();
         }
-        headers["Content-Length"] = std::to_string(content_length);
+        headers["Content-Length"] = asprintf("%d", content_length);
     }
     else {
         content_length = atoi(iter->second.c_str());
@@ -76,6 +82,7 @@ void HttpPayload::DumpBody() {
         return;
     }
     FillContentType();
+#ifndef WITHOUT_HTTP_CONTENT
     switch(content_type) {
     case APPLICATION_JSON:
         body = dump_json(json);
@@ -101,6 +108,7 @@ void HttpPayload::DumpBody() {
         // nothing to do
         break;
     }
+#endif
 }
 
 int HttpPayload::ParseBody() {
@@ -108,6 +116,7 @@ int HttpPayload::ParseBody() {
         return -1;
     }
     FillContentType();
+#ifndef WITHOUT_HTTP_CONTENT
     switch(content_type) {
     case APPLICATION_JSON:
         return parse_json(body.c_str(), json);
@@ -130,6 +139,8 @@ int HttpPayload::ParseBody() {
         // nothing to do
         return 0;
     }
+#endif
+    return 0;
 }
 
 std::string HttpPayload::Dump(bool is_dump_headers, bool is_dump_body) {

+ 5 - 1
http/HttpPayload.h

@@ -4,9 +4,9 @@
 #include <string>
 #include <map>
 
+#include "hstring.h"
 #include "httpdef.h"
 #include "http_content.h"
-#include "hstring.h"
 
 typedef std::map<std::string, std::string, StringCaseLess>  http_headers;
 typedef std::string                                         http_body;
@@ -24,9 +24,11 @@ public:
     void*               content;    // DATA_NO_COPY
     int                 content_length;
     http_content_type   content_type;
+#ifndef WITHOUT_HTTP_CONTENT
     Json                json;       // APPLICATION_JSON
     MultiPart           mp;         // FORM_DATA
     KeyValue            kv;         // X_WWW_FORM_URLENCODED
+#endif
 
     HttpPayload() {
         type = HTTP_BOTH;
@@ -47,9 +49,11 @@ public:
         Init();
         headers.clear();
         body.clear();
+#ifndef WITHOUT_HTTP_CONTENT
         json.clear();
         mp.clear();
         kv.clear();
+#endif
     }
 
     // structured-content -> content_type <-> headers Content-Type

+ 2 - 0
http/http_content.cpp

@@ -143,6 +143,7 @@ int parse_query_params(const char* query_string, QueryParams& query_params) {
     return query_params.size() == 0 ? -1 : 0;
 }
 
+#ifndef WITHOUT_HTTP_CONTENT
 std::string dump_json(Json& json) {
     return json.dump();
 }
@@ -325,3 +326,4 @@ int parse_multipart(std::string& str, MultiPart& mp, const char* boundary) {
     multipart_parser_free(parser);
     return nparse == str.size() ? 0 : -1;
 }
+#endif

+ 5 - 0
http/http_content.h

@@ -52,6 +52,9 @@ typedef KeyValue    QueryParams;
 std::string dump_query_params(QueryParams& query_params);
 int         parse_query_params(const char* query_string, QueryParams& query_params);
 
+// NOTE: WITHOUT_HTTP_CONTENT
+// ndk-r10e no std::to_string and can't compile modern json.hpp
+#ifndef WITHOUT_HTTP_CONTENT
 // Json
 #include "json.hpp"
 using Json = nlohmann::json;
@@ -105,4 +108,6 @@ typedef MAP<std::string, FormData>          MultiPart;
 std::string dump_multipart(MultiPart& mp, const char* boundary = DEFAULT_MULTIPART_BOUNDARY);
 int         parse_multipart(std::string& str, MultiPart& mp, const char* boundary);
 
+#endif
+
 #endif // HTTP_CONTENT_H_

+ 2 - 1
http/server/HttpHandler.cpp

@@ -1,5 +1,6 @@
 #include "HttpHandler.h"
 
+#include "hstring.h"
 #include "http_page.h"
 
 int HttpHandler::HandleRequest() {
@@ -94,7 +95,7 @@ make_http_status_page:
             res.headers["Content-Type"] = fc->content_type;
             res.FillContentType();
         }
-        res.headers["Content-Length"] = std::to_string(res.content_length);
+        res.headers["Content-Length"] = asprintf("%d", res.content_length);
         res.headers["Last-Modified"] = fc->last_modified;
         res.headers["Etag"] = fc->etag;
     }