From 1a527878684d7a7edb84322e5e421ca3dd60530d Mon Sep 17 00:00:00 2001 From: yang Date: Sun, 24 May 2020 01:24:00 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=B8=80=E4=BA=9B=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2=E9=95=BF=E5=BA=A6=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- chap-30/CMakeLists.txt | 3 +++ chap-34/http_server01.c | 14 ++++++-------- lib/http_request.c | 1 + lib/http_server.c | 6 +++--- lib/tcp_connection.c | 3 +++ 5 files changed, 16 insertions(+), 11 deletions(-) diff --git a/chap-30/CMakeLists.txt b/chap-30/CMakeLists.txt index b7c3d40..51d8031 100644 --- a/chap-30/CMakeLists.txt +++ b/chap-30/CMakeLists.txt @@ -1,2 +1,5 @@ add_executable(aio01 aio01.c) target_link_libraries(aio01 yolanda) +IF (CMAKE_SYSTEM_NAME MATCHES "Linux") + target_link_libraries(aio01 rt) +ENDIF (CMAKE_SYSTEM_NAME MATCHES "Linux") diff --git a/chap-34/http_server01.c b/chap-34/http_server01.c index 535b867..d5a11be 100644 --- a/chap-34/http_server01.c +++ b/chap-34/http_server01.c @@ -6,22 +6,20 @@ //数据读到buffer之后的callback int onRequest(struct http_request *httpRequest, struct http_response *httpResponse) { char *url = httpRequest->url; - char *question = memmem(url, strlen(url), "?", 1); - char *path = NULL; + char *question = strstr(url, "?"); + int url_len; if (question != NULL) { - path = malloc(question - url); - strncpy(path, url, question - url); + url_len = question - url; } else { - path = malloc(strlen(url)); - strncpy(path, url, strlen(url)); + url_len = strlen(url); } - if (strcmp(path, "/") == 0) { + if (strncmp(url, "/", url_len) == 0) { httpResponse->statusCode = OK; httpResponse->statusMessage = "OK"; httpResponse->contentType = "text/html"; httpResponse->body = "This is network programming

Hello, network programming

"; - } else if (strcmp(path, "/network") == 0) { + } else if (strncmp(url, "/network", url_len) == 0) { httpResponse->statusCode = OK; httpResponse->statusMessage = "OK"; httpResponse->contentType = "text/plain"; diff --git a/lib/http_request.c b/lib/http_request.c index 99f2bfe..2e6b090 100644 --- a/lib/http_request.c +++ b/lib/http_request.c @@ -75,6 +75,7 @@ int http_request_close_connection(struct http_request *httpRequest) { if (httpRequest->version != NULL && strncmp(httpRequest->version, HTTP10, strlen(HTTP10)) == 0 && + connection != NULL && strncmp(connection, KEEP_ALIVE, strlen(KEEP_ALIVE)) == 1) { return 1; } diff --git a/lib/http_server.c b/lib/http_server.c index cba0507..cfe4f61 100644 --- a/lib/http_server.c +++ b/lib/http_server.c @@ -18,7 +18,7 @@ int process_status_line(char *start, char *end, struct http_request *httpRequest int method_size = space - start; httpRequest->method = malloc(method_size + 1); strncpy(httpRequest->method, start, space - start); - httpRequest->method[method_size + 1] = '\0'; + httpRequest->method[method_size] = '\0'; //url start = space + 1; @@ -27,13 +27,13 @@ int process_status_line(char *start, char *end, struct http_request *httpRequest int url_size = space - start; httpRequest->url = malloc(url_size + 1); strncpy(httpRequest->url, start, space - start); - httpRequest->url[url_size + 1] = '\0'; + httpRequest->url[url_size] = '\0'; //version start = space + 1; httpRequest->version = malloc(end - start + 1); strncpy(httpRequest->version, start, end - start); - httpRequest->version[end - start + 1] = '\0'; + httpRequest->version[end - start] = '\0'; assert(space != NULL); return size; } diff --git a/lib/tcp_connection.c b/lib/tcp_connection.c index 3b5240a..58915f4 100644 --- a/lib/tcp_connection.c +++ b/lib/tcp_connection.c @@ -6,6 +6,9 @@ int handle_connection_closed(struct tcp_connection *tcpConnection) { struct event_loop *eventLoop = tcpConnection->eventLoop; struct channel *channel = tcpConnection->channel; event_loop_remove_channel_event(eventLoop, channel->fd, channel); + if(close(channel->fd) < 0) { + yolanda_msgx("close fd %d failed, errno : %d", channel->fd, errno); + } if (tcpConnection->connectionClosedCallBack != NULL) { tcpConnection->connectionClosedCallBack(tcpConnection); }