hewei 6 лет назад
Родитель
Сommit
098b97362e

+ 1 - 0
Makefile

@@ -13,6 +13,7 @@ prepare:
 	-rm base/RAII.o
 
 libhv: prepare
+	$(MAKEF) TARGET=$@ TARGET_TYPE=SHARED SRCDIRS=". base utils event http http/client http/server protocol"
 	$(MAKEF) TARGET=$@ TARGET_TYPE=STATIC SRCDIRS=". base utils event http http/client http/server protocol"
 
 test: prepare

+ 22 - 5
Makefile.in

@@ -60,15 +60,32 @@ endif
 
 # CFLAGS, CXXFLAGS, ARFLAGS
 ifeq ($(BUILD_TYPE), DEBUG)
-	DEFAULT_CFLAGS = -g
+	DEFAULT_CFLAGS = -g -Wall
 endif
-DEFAULT_CFLAGS += -Wall -O3 -fPIC -fvisibility=hidden
+DEFAULT_CFLAGS += -O3 -fPIC
+
 ifndef CFLAGS
-CFLAGS := $(DEFAULT_CFLAGS) -std=c99 $(MAKE_CFLAGS)
+CFLAGS := $(DEFAULT_CFLAGS) -std=c99
+else
+ifeq ($(findstring -fPIC, $(CFLAGS)), )
+override CFLAGS += -fPIC
+endif
+ifeq ($(findstring -std, $(CFLAGS)), )
+override CFLAGS += -std=c99
 endif
+endif
+
 ifndef CXXFLAGS
-CXXFLAGS := $(DEFAULT_CFLAGS) -std=c++11 $(MAKE_CXXFLAGS)
+CXXFLAGS := $(DEFAULT_CFLAGS) -std=c++11
+else
+ifeq ($(findstring -fPIC, $(CXXFLAGS)), )
+override CXXFLAGS += -fPIC
+endif
+ifeq ($(findstring -std, $(CXXFLAGS)), )
+override CXXFLAGS += -std=c++11
 endif
+endif
+
 ifndef ARFLAGS
 ARFLAGS := cr
 endif
@@ -100,7 +117,7 @@ endif
 endif
 
 ifeq ($(TARGET_TYPE), STATIC)
-	CPPFLAGS += -DBUILD_STATIC_LIB
+	CPPFLAGS += -DHV_STATICLIB
 endif
 
 ifeq ($(BUILD_TYPE), DEBUG)

+ 5 - 2
README.md

@@ -58,13 +58,13 @@ git clone https://github.com/ithewei/libhv.git
 cd libhv
 make httpd curl
 
-bin/httpd -d
+bin/httpd -s restart -d
 ps aux | grep httpd
 
 # http web service
 bin/curl -v localhost:8080
 
-# indexof
+# http indexof service
 bin/curl -v localhost:8080/downloads/
 
 # http api service
@@ -75,6 +75,9 @@ make webbench
 bin/webbench -c 2 -t 60 localhost:8080
 ```
 
+**libhv(port:8080) vs nginx(port:80)**
+![libhv-vs-nginx.png](html/downloads/libhv-vs-nginx.png)
+
 ### event-loop
 see examples/tcp.c
 ```

+ 6 - 6
base/hplatform.h

@@ -69,18 +69,18 @@
 #endif
 // __clang__
 
-#ifdef BUILD_STATIC_LIB
-#define EXPORT
+#ifdef HV_STATICLIB
+#define HEXPORT
 #elif defined(OS_WIN)
 #ifdef DLL_EXPORTS
-#define EXPORT  __declspec(dllexport)
+#define HEXPORT  __declspec(dllexport)
 #else
-#define EXPORT  __declspec(dllimport)
+#define HEXPORT  __declspec(dllimport)
 #endif
 #elif defined(__GNUC__)
-#define EXPORT  __attribute__((visibility("default")))
+#define HEXPORT  __attribute__((visibility("default")))
 #else
-#define EXPORT
+#define HEXPORT
 #endif
 
 // ARCH

+ 1 - 1
etc/httpd.conf

@@ -1,6 +1,6 @@
 # [root]
 
-# logfile = logs/test.log
+# logfile = logs/httpd.log
 # loglevel = [VERBOSE,DEBUG,INFO,WARN,ERROR,FATAL,SILENT]
 loglevel = INFO
 log_remain_days = 3

+ 4 - 0
event/nio.c

@@ -85,11 +85,13 @@ accept:
     if (io->io_type != HIO_TYPE_SSL) {
         // NOTE: SSL call accept_cb after handshark finished
         if (io->accept_cb) {
+            /*
             char localaddrstr[SOCKADDR_STRLEN] = {0};
             char peeraddrstr[SOCKADDR_STRLEN] = {0};
             printd("accept listenfd=%d connfd=%d [%s] <= [%s]\n", io->fd, connfd,
                     SOCKADDR_STR(io->localaddr, localaddrstr),
                     SOCKADDR_STR(io->peeraddr, peeraddrstr));
+            */
             //printd("accept_cb------\n");
             io->accept_cb(connio);
             //printd("accept_cb======\n");
@@ -113,11 +115,13 @@ static void nio_connect(hio_t* io) {
     else {
         addrlen = sizeof(sockaddr_un);
         getsockname(io->fd, io->localaddr, &addrlen);
+        /*
         char localaddrstr[SOCKADDR_STRLEN] = {0};
         char peeraddrstr[SOCKADDR_STRLEN] = {0};
         printd("connect connfd=%d [%s] => [%s]\n", io->fd,
                 SOCKADDR_STR(io->localaddr, localaddrstr),
                 SOCKADDR_STR(io->peeraddr, peeraddrstr));
+        */
 #ifdef WITH_OPENSSL
         if (io->io_type == HIO_TYPE_SSL) {
             SSL_CTX* ssl_ctx = (SSL_CTX*)g_ssl_ctx;

+ 2 - 0
event/nlog.c

@@ -40,12 +40,14 @@ static void on_read(hio_t* io, void* buf, int readbytes) {
 }
 
 static void on_accept(hio_t* io) {
+    /*
     printd("on_accept connfd=%d\n", hio_fd(io));
     char localaddrstr[SOCKADDR_STRLEN] = {0};
     char peeraddrstr[SOCKADDR_STRLEN] = {0};
     printd("accept connfd=%d [%s] <= [%s]\n", hio_fd(io),
             SOCKADDR_STR(hio_localaddr(io), localaddrstr),
             SOCKADDR_STR(hio_peeraddr(io), peeraddrstr));
+    */
 
     static char s_readbuf[256] = {0};
     hio_set_readbuf(io, s_readbuf, sizeof(s_readbuf));

+ 8 - 6
event/nmap.cpp

@@ -29,12 +29,14 @@ static void on_timer(htimer_t* timer) {
 }
 
 static void on_recvfrom(hio_t* io, void* buf, int readbytes) {
-    //printf("on_recv fd=%d readbytes=%d\n", io->fd, readbytes);
-    //char localaddrstr[SOCKADDR_STRLEN] = {0};
-    //char peeraddrstr[SOCKADDR_STRLEN] = {0};
-    //printf("[%s] <=> [%s]\n",
-            //SOCKADDR_STR(hio_localaddr(io), localaddrstr),
-            //SOCKADDR_STR(hio_peeraddr(io), peeraddrstr));
+    //printd("on_recv fd=%d readbytes=%d\n", io->fd, readbytes);
+    /*
+    char localaddrstr[SOCKADDR_STRLEN] = {0};
+    char peeraddrstr[SOCKADDR_STRLEN] = {0};
+    printd("[%s] <=> [%s]\n",
+            SOCKADDR_STR(hio_localaddr(io), localaddrstr),
+            SOCKADDR_STR(hio_peeraddr(io), peeraddrstr));
+    */
     nmap_udata_t* udata = (nmap_udata_t*)io->loop->userdata;
     if (++udata->recv_cnt == udata->send_cnt) {
         //hloop_stop(io->loop);

+ 0 - 246
html/downloads/README.md

@@ -1,246 +0,0 @@
-[![Build Status](https://travis-ci.org/ithewei/libhv.svg?branch=master)](https://travis-ci.org/ithewei/libhv)
-
-## Intro
-
-Like `libevent, libev, and libuv`,
-`libhv` provides event-loop with non-blocking IO and timer,
-but simpler apis and richer protocols.
-
-## Features
-
-- cross-platform (Linux, Windows, Mac)
-- event-loop (IO, timer, idle)
-- http client/server (include https http1/x http2 grpc)
-- protocols
-    - dns
-    - ftp
-    - smtp
-- apps
-    - ls
-    - ifconfig
-    - ping
-    - nc
-    - nmap
-    - nslookup
-    - ftp
-    - sendmail
-    - httpd
-    - curl
-
-## Getting Started
-
-### http
-see examples/httpd.cpp
-```
-#include "HttpServer.h"
-
-int http_api_hello(HttpRequest* req, HttpResponse* res) {
-    res->body = "hello";
-    return 0;
-}
-
-int main() {
-    HttpService service;
-    service.base_url = "/v1/api";
-    service.AddApi("/hello", HTTP_GET, http_api_hello);
-
-    http_server_t server;
-    server.port = 8080;
-    server.worker_processes = 4;
-    server.service = &service;
-    http_server_run(&server);
-    return 0;
-}
-```
-
-```shell
-git clone https://github.com/ithewei/libhv.git
-cd libhv
-make httpd curl
-
-bin/httpd -d
-ps aux | grep httpd
-
-# http web service
-bin/curl -v localhost:8080
-
-# indexof
-bin/curl -v localhost:8080/downloads/
-
-# http api service
-bin/curl -v -X POST localhost:8080/v1/api/json -H "Content-Type:application/json" -d '{"user":"admin","pswd":"123456"}'
-
-# webbench (linux only)
-make webbench
-bin/webbench -c 2 -t 60 localhost:8080
-```
-
-### event-loop
-see examples/tcp.c
-```
-#include "hloop.h"
-#include "hsocket.h"
-
-#define RECV_BUFSIZE    8192
-static char recvbuf[RECV_BUFSIZE];
-
-void on_close(hio_t* io) {
-    printf("on_close fd=%d error=%d\n", hio_fd(io), hio_error(io));
-}
-
-void on_recv(hio_t* io, void* buf, int readbytes) {
-    printf("on_recv fd=%d readbytes=%d\n", hio_fd(io), readbytes);
-    char localaddrstr[SOCKADDR_STRLEN] = {0};
-    char peeraddrstr[SOCKADDR_STRLEN] = {0};
-    printf("[%s] <=> [%s]\n",
-            SOCKADDR_STR(hio_localaddr(io), localaddrstr),
-            SOCKADDR_STR(hio_peeraddr(io), peeraddrstr));
-    printf("< %s\n", buf);
-    // echo
-    printf("> %s\n", buf);
-    hio_write(io, buf, readbytes);
-}
-
-void on_accept(hio_t* io) {
-    printf("on_accept connfd=%d\n", hio_fd(io));
-    char localaddrstr[SOCKADDR_STRLEN] = {0};
-    char peeraddrstr[SOCKADDR_STRLEN] = {0};
-    printf("accept connfd=%d [%s] <= [%s]\n", hio_fd(io),
-            SOCKADDR_STR(hio_localaddr(io), localaddrstr),
-            SOCKADDR_STR(hio_peeraddr(io), peeraddrstr));
-
-    hio_setcb_close(io, on_close);
-    hio_setcb_read(io, on_recv);
-    hio_set_readbuf(io, recvbuf, RECV_BUFSIZE);
-    hio_read(io);
-}
-
-int main(int argc, char** argv) {
-    if (argc < 2) {
-        printf("Usage: cmd port\n");
-        return -10;
-    }
-    int port = atoi(argv[1]);
-
-    hloop_t* loop = hloop_new(0);
-    hio_t* listenio = create_tcp_server(loop, "0.0.0.0", port, on_accept);
-    if (listenio == NULL) {
-        return -20;
-    }
-    printf("listenfd=%d\n", hio_fd(listenio));
-    hloop_run(loop);
-    hloop_free(&loop);
-    return 0;
-}
-```
-```
-make tcp udp nc
-bin/tcp 1111
-bin/nc 1111
-
-bin/udp 2222
-bin/nc -u 2222
-```
-
-## BUILD
-
-### lib
-- make libhv
-- make install
-
-### examples
-- make test # master-workers model
-- make timer # timer add/del/reset
-- make loop # event-loop(include idle, timer, io)
-- make tcp  # tcp server
-- make udp  # udp server
-- make nc   # network client
-- make nmap # host discovery
-- make httpd # http server
-- make curl # http client
-
-### unittest
-- make unittest
-
-### compile options
-#### compile with print debug info
-- make DEFINES=PRINT_DEBUG
-
-#### compile WITH_OPENSSL
-- make DEFINES=WITH_OPENSSL
-
-#### compile WITH_CURL
-- make DEFINES="WITH_CURL CURL_STATICLIB"
-
-#### compile WITH_NGHTTP2
-- make DEFINES=WITH_NGHTTP2
-
-#### other options
-- ENABLE_IPV6
-- WITH_WINDUMP
-- USE_MULTIMAP
-
-## Module
-
-### data-structure
-- array.h:       动态数组
-- list.h:        链表
-- queue.h:       队列
-- heap.h:        堆
-
-### base
-- hplatform.h:   平台相关宏
-- hdef.h:        宏定义
-- hversion.h:    版本
-- hbase.h:       基本接口
-- hsysinfo.h:    系统信息
-- hproc.h:       子进程/线程类
-- hmath.h:       math扩展函数
-- htime.h:       时间
-- herr.h:        错误码
-- hlog.h:        日志
-- hmutex.h:     同步锁
-- hthread.h:    线程
-- hsocket.h:     socket操作
-- hbuf.h:        缓存类
-- hurl.h:        URL转义
-- hgui.h:        gui相关定义
-- hstring.h:     字符串
-- hvar.h:        var变量
-- hobj.h:        对象基类
-- hfile.h:       文件类
-- hdir.h:        ls实现
-- hscope.h:      作用域RAII机制
-- hthreadpool.h: 线程池
-- hobjectpool.h: 对象池
-
-### utils
-- hmain.h:       main_ctx: arg env
-- hendian.h:     大小端
-- ifconfig.h:    ifconfig实现
-- iniparser.h:   ini解析
-- singleton.h:   单例模式
-- md5.h
-- base64.h
-- json.hpp
-
-### event
-- hloop.h:       事件循环
-
-#### iowatcher
-- EVENT_SELECT
-- EVENT_POLL
-- EVENT_EPOLL   (linux only)
-- EVENT_KQUEUE  (mac/bsd)
-- EVENT_IOCP    (windows only)
-
-### http
-- http_client.h: http客户端
-- HttpServer.h:  http服务端
-
-### other
-
-- hv.h:         总头文件
-- Makefile.in:   通用Makefile模板
-- main.cpp.tmpl: 通用main.cpp模板
-

BIN
html/downloads/libhv-vs-nginx.png


+ 2 - 2
html/downloads/scripts/getting_started.sh

@@ -2,13 +2,13 @@
 
 make httpd curl
 
-bin/httpd -d
+bin/httpd -s restart -d
 ps aux | grep httpd
 
 # http web service
 bin/curl -v localhost:8080
 
-# indexof
+# http indexof service
 bin/curl -v localhost:8080/downloads/
 
 # http api service

+ 1 - 0
http/client/http_client.cpp

@@ -379,6 +379,7 @@ connect:
     cli->session->SubmitRequest(req);
     char recvbuf[1024] = {0};
     int total_nsend, nsend, nrecv;
+    total_nsend = nsend = nrecv = 0;
 send:
     char* data = NULL;
     size_t len  = 0;

+ 1 - 12
http/server/FileCache.cpp

@@ -1,7 +1,6 @@
 #include "FileCache.h"
 
 #include "hscope.h"
-#include "md5.h" // etag
 
 #include "httpdef.h" // for http_content_type_str_by_suffix
 #include "http_page.h" //make_index_of_page
@@ -66,17 +65,7 @@ file_cache_t* FileCache::Open(const char* filepath, void* ctx) {
         }
         time_t tt = fc->st.st_mtime;
         strftime(fc->last_modified, sizeof(fc->last_modified), "%a, %d %b %Y %H:%M:%S GMT", gmtime(&tt));
-        MD5_CTX md5_ctx;
-        MD5Init(&md5_ctx);
-        MD5Update(&md5_ctx, (unsigned char*)fc->filebuf.base, fc->filebuf.len);
-        unsigned char digital[16];
-        MD5Final(digital, &md5_ctx);
-        char* md5 = fc->etag;
-        for (int i = 0; i < 16; ++i) {
-            sprintf(md5, "%02x", digital[i]);
-            md5 += 2;
-        }
-        fc->etag[32] = '\0';
+        snprintf(fc->etag, sizeof(fc->etag), "\"%zx-%zx\"", fc->st.st_mtime, fc->st.st_size);
     }
     return fc;
 }

+ 1 - 1
http/server/HttpServer.cpp

@@ -145,7 +145,7 @@ static void on_recv(hio_t* io, void* _buf, int readbytes) {
 #endif
 
 handle_request:
-    int ret = handler->HandleRequest();
+    handler->HandleRequest();
     // prepare headers body
     // Server:
     static char s_Server[64] = {'\0'};