浏览代码

Add some interfaces

ithewei 3 年之前
父节点
当前提交
7224b8f749
共有 3 个文件被更改,包括 29 次插入2 次删除
  1. 6 1
      examples/httpd/handler.cpp
  2. 14 1
      http/HttpMessage.h
  3. 9 0
      http/server/HttpContext.h

+ 6 - 1
examples/httpd/handler.cpp

@@ -17,7 +17,7 @@ int Handler::preprocessor(HttpRequest* req, HttpResponse* resp) {
     // 301
     if (req->scheme == "http") {
         std::string location = hv::asprintf("https://%s:%d%s", req->host.c_str(), 8443, req->path.c_str());
-        return resp->Redirect(location);
+        return resp->Redirect(location, HTTP_STATUS_MOVED_PERMANENTLY);
     }
 #endif
 
@@ -229,6 +229,11 @@ int Handler::recvLargeFile(const HttpContextPtr& ctx, http_parser_state state, c
     switch (state) {
     case HP_HEADERS_COMPLETE:
         {
+            if (ctx->is(MULTIPART_FORM_DATA)) {
+                // NOTE: You can use multipart_parser if you want to use multipart/form-data.
+                ctx->close();
+                return HTTP_STATUS_BAD_REQUEST;
+            }
             std::string save_path = "html/uploads/";
             std::string filename = ctx->param("filename", "unnamed.txt");
             std::string filepath = save_path + filename;

+ 14 - 1
http/HttpMessage.h

@@ -508,9 +508,22 @@ public:
     }
     void FillHost(const char* host, int port = DEFAULT_HTTP_PORT);
     void SetHost(const char* host, int port = DEFAULT_HTTP_PORT);
+
     void SetProxy(const char* host, int port);
     bool IsProxy() { return proxy; }
 
+    void SetTimeout(int sec) { timeout = sec; }
+    void SetConnectTimeout(int sec) { connect_timeout = sec; }
+
+    void AllowRedirect(bool on = true) { redirect = on; }
+
+    // NOTE: SetRetry just for AsyncHttpClient
+    void SetRetry(int count = DEFAULT_HTTP_FAIL_RETRY_COUNT,
+                  int delay = DEFAULT_HTTP_FAIL_RETRY_DELAY) {
+        retry_count = count;
+        retry_delay = delay;
+    }
+
     // Range: bytes=0-4095
     void SetRange(long from = 0, long to = -1) {
         headers["Range"] = hv::asprintf("bytes=%ld-%ld", from, to);
@@ -563,7 +576,7 @@ public:
         return false;
     }
 
-    int Redirect(const std::string& location, http_status status = HTTP_STATUS_MOVED_PERMANENTLY) {
+    int Redirect(const std::string& location, http_status status = HTTP_STATUS_FOUND) {
         status_code = status;
         headers["Location"] = location;
         return status_code;

+ 9 - 0
http/server/HttpContext.h

@@ -26,6 +26,10 @@ struct HV_EXPORT HttpContext {
         return request->client_addr.ip;
     }
 
+    int port() {
+        return request->client_addr.port;
+    }
+
     http_method method() {
         return request->method;
     }
@@ -192,6 +196,11 @@ struct HV_EXPORT HttpContext {
     }
 #endif
 
+    int redirect(const std::string& location, http_status status = HTTP_STATUS_FOUND) {
+        response->Redirect(location, status);
+        return send();
+    }
+
     int close() {
         return writer ? writer->close(true) : -1;
     }