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 programmingHello, 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);
}