ithewei 6 년 전
부모
커밋
032940b3ec
7개의 변경된 파일98개의 추가작업 그리고 85개의 파일을 삭제
  1. 13 3
      README.md
  2. 1 0
      h.h
  3. 0 29
      http/HttpServer.h
  4. 1 1
      http/http_content.h
  5. 28 39
      http/http_server.cpp
  6. 45 0
      http/http_server.h
  7. 10 13
      http/httpd.cpp.demo

+ 13 - 3
README.md

@@ -14,7 +14,7 @@ hw 是一套跨平台c++工具集,类名以H开头
 
 ## Module
 
-- h.h:总头文件
+### base
 - hplatform.h: 平台相关
 - hdef.h: 宏定义
 - hversion.h: 版本
@@ -33,19 +33,29 @@ hw 是一套跨平台c++工具集,类名以H开头
 - hmutex.h:同步锁
 - hthread.h:线程
 - hthreadpool.h:线程池
+
+### utils
 - hendian.h: 大小端
 - hmain.h: main_ctx: arg env
 - ifconfig.h: ifconfig实现
 - singleton.h: 单例模式
 - iniparser.h: ini解析
 
-## other
+### event
+- hloop.h: 事件循环
+
+### http
+- http_client.h: http客户端
+- http_server.h: http服务端
 
+### other
+
+- h.h:总头文件
 - Makefile.in: 通用Makefile模板
 - main.cpp.tmpl: 通用main.cpp模板
 
 ## BUILD
 
 ```
-make test
+make all
 ```

+ 1 - 0
h.h

@@ -37,6 +37,7 @@
 
 //--------------------utils-----------------------------
 #ifdef WITH_HW_UTILS
+#include "md5.h"
 #include "base64.h"
 #include "hbytearray.h"
 #include "hframe.h"

+ 0 - 29
http/HttpServer.h

@@ -1,29 +0,0 @@
-#ifndef HTTP_SERVER_H_
-#define HTTP_SERVER_H_
-
-#include "HttpService.h"
-
-class HttpServer {
-public:
-    HttpServer();
-
-    int SetListenPort(int port);
-
-    void SetWorkerProcesses(int worker_processes) {
-        this->worker_processes = worker_processes;
-    }
-
-    void RegisterService(HttpService* service) {
-        this->service = service;
-    }
-
-    void Run(bool wait = true);
-
-public:
-    int port;
-    int worker_processes;
-    HttpService* service;
-    int listenfd_;
-};
-
-#endif

+ 1 - 1
http/http_content.h

@@ -39,7 +39,7 @@ public:
 
 // MAP
 #ifdef USE_MULTIMAP
-#define MAP     std::Multipart
+#define MAP     std::MultiMap
 #else
 #define MAP     std::map
 #endif

+ 28 - 39
http/HttpServer.cpp → http/http_server.cpp

@@ -1,15 +1,9 @@
-#include "HttpServer.h"
+#include "http_server.h"
 
+#include "h.h"
 #include "hmain.h"
-#include "hversion.h"
-#include "htime.h"
-#include "hsocket.h"
-#include "hbuf.h"
-#include "hlog.h"
-#include "hscope.h"
-#include "hfile.h"
-
 #include "hloop.h"
+
 #include "HttpParser.h"
 #include "FileCache.h"
 #include "httpd_conf.h"
@@ -17,6 +11,7 @@
 #define RECV_BUFSIZE    4096
 #define SEND_BUFSIZE    4096
 
+static HttpService s_default_service;
 static FileCache s_filecache;
 
 /*
@@ -72,7 +67,7 @@ static void worker_init(void* userdata) {
 }
 
 struct http_connect_userdata {
-    HttpServer*             server;
+    http_server_t*          server;
     std::string             log;
     HttpParser              parser;
     HttpRequest             req;
@@ -280,7 +275,7 @@ accept:
         // new http_connect
         // delete on on_read
         http_connect_userdata* hcu = new http_connect_userdata;
-        hcu->server = (HttpServer*)userdata;
+        hcu->server = (http_server_t*)userdata;
         hcu->log += asprintf("[%s:%d]", inet_ntoa(peeraddr.sin_addr), ntohs(peeraddr.sin_port));
 
         nonblocking(connfd);
@@ -316,8 +311,8 @@ void handle_cached_files(htimer_t* timer, void* userdata) {
 }
 
 static void worker_proc(void* userdata) {
-    HttpServer* server = (HttpServer*)userdata;
-    int listenfd = server->listenfd_;
+    http_server_t* server = (http_server_t*)userdata;
+    int listenfd = server->listenfd;
     hloop_t loop;
     hloop_init(&loop);
     htimer_add(&loop, handle_cached_files, &s_filecache, MAX(60000, g_conf_ctx.file_cached_time*1000));
@@ -325,51 +320,45 @@ static void worker_proc(void* userdata) {
     hloop_run(&loop);
 }
 
-static HttpService s_default_service;
-#define DEFAULT_HTTP_PORT   80
-HttpServer::HttpServer() {
-    port = DEFAULT_HTTP_PORT;
-    worker_processes = 0;
-    service = NULL;
-    listenfd_ = 0;
-}
-
-int HttpServer::SetListenPort(int port) {
-    this->port = port;
-    listenfd_ = Listen(port);
-    return listenfd_ < 0 ? listenfd_ : 0;
-}
-
-void HttpServer::Run(bool wait) {
-    if (service == NULL) {
-        service = &s_default_service;
+int http_server_run(http_server_t* server, int wait) {
+    // worker_processes
+    if (server->worker_processes != 0 && g_worker_processes_num != 0 && g_worker_processes != NULL) {
+        return ERR_OVER_LIMIT;
     }
-    if (worker_processes == 0) {
-        worker_proc(this);
+    // service
+    if (server->service == NULL) {
+        server->service = &s_default_service;
+    }
+    // port
+    server->listenfd = Listen(server->port);
+    if (server->listenfd < 0) return server->listenfd;
+
+    if (server->worker_processes == 0) {
+        worker_proc(server);
     }
     else {
         // master-workers processes
-        g_worker_processes_num = worker_processes;
-        int bytes = worker_processes * sizeof(proc_ctx_t);
+        g_worker_processes_num = server->worker_processes;
+        int bytes = g_worker_processes_num * sizeof(proc_ctx_t);
         g_worker_processes = (proc_ctx_t*)malloc(bytes);
         if (g_worker_processes == NULL) {
             perror("malloc");
             abort();
         }
         memset(g_worker_processes, 0, bytes);
-        for (int i = 0; i < worker_processes; ++i) {
+        for (int i = 0; i < g_worker_processes_num; ++i) {
             proc_ctx_t* ctx = g_worker_processes + i;
             ctx->init = worker_init;
             ctx->init_userdata = NULL;
             ctx->proc = worker_proc;
-            ctx->proc_userdata = this;
+            ctx->proc_userdata = server;
             spawn_proc(ctx);
         }
     }
 
     if (wait) {
         master_init(NULL);
-        master_proc(this);
+        master_proc(NULL);
     }
+    return 0;
 }
-

+ 45 - 0
http/http_server.h

@@ -0,0 +1,45 @@
+#ifndef HTTP_SERVER_H_
+#define HTTP_SERVER_H_
+
+#include "HttpService.h"
+
+#define DEFAULT_HTTP_PORT   80
+typedef struct http_server_s {
+    int port;
+    int worker_processes;
+    HttpService* service;
+//private:
+    int listenfd;
+
+#ifdef __cplusplus
+    http_server_s() {
+        port = DEFAULT_HTTP_PORT;
+        worker_processes = 0;
+        service = NULL;
+        listenfd = -1;
+    }
+#endif
+} http_server_t;
+
+/*
+#include "http_server.h"
+
+void http_api_hello(HttpRequest* req, HttpResponse* res) {
+    res->body = "hello";
+}
+
+int main() {
+    HttpService service;
+    service.AddApi("/hello", http_api_hello);
+
+    http_server_t server;
+    server.port = 8080;
+    server.worker_processes = 4;
+    server.service = &service;
+    http_server_run(&server);
+    return 0;
+}
+ */
+int http_server_run(http_server_t* server, int wait = 1);
+
+#endif

+ 10 - 13
http/httpd.cpp.demo

@@ -1,8 +1,8 @@
 #include "h.h"
 #include "hmain.h"
-#include "HttpServer.h"
-#include "api_test.h"
 #include "httpd_conf.h"
+#include "http_server.h"
+#include "api_test.h"
 
 httpd_conf_ctx_t g_conf_ctx;
 HttpService g_http_service;
@@ -233,22 +233,19 @@ int main(int argc, char** argv) {
     // pidfile
     create_pidfile();
 
-    // HttpServer
-    HttpServer srv;
-    ret = srv.SetListenPort(g_conf_ctx.port);
-    if (ret < 0) {
-        return ret;
-    }
-    srv.SetWorkerProcesses(g_conf_ctx.worker_processes);
-    //g_http_service.AddApi("query", HTTP_GET, http_api_query);
+    // HttpService
     g_http_service.preprocessor = http_api_preprocessor;
     g_http_service.postprocessor = http_api_postprocessor;
 #define XXX(path, method, handler) \
     g_http_service.AddApi(path, HTTP_##method, handler);
     HTTP_API_MAP(XXX)
 #undef XXX
-    srv.RegisterService(&g_http_service);
-    srv.Run();
 
-    return 0;
+    // http_server
+    http_server_t srv;
+    srv.port = g_conf_ctx.port;
+    srv.worker_processes = g_conf_ctx.worker_processes;
+    srv.service = &g_http_service;
+    ret = http_server_run(&srv);
+    return ret;
 }