diff --git a/DesignedWorlds b/DesignedWorlds index 4cfa879..d5ef02e 100755 Binary files a/DesignedWorlds and b/DesignedWorlds differ diff --git a/source/http.c b/source/http.c index 9312bd3..b118d46 100644 --- a/source/http.c +++ b/source/http.c @@ -94,50 +94,55 @@ void HttpResponseHeaderToS(HttpResponseHeader* rh,char* d, char* buffer, int buf } -void HttpRequestHeaderFromS(HttpRequestHeader* rh, char* buffer) { - - int methodLen = 0; - char methodStr[8] = ""; - if (strstr(buffer, "GET") != NULL) { - rh->method = HTTP_GET; - } else if (strstr(buffer, "POST") != NULL) { - rh->method = HTTP_POST; - } else if (strstr(buffer, "PUT") != NULL) { - rh->method = HTTP_PUT; - } else if (strstr(buffer, "DELETE") != NULL) { - rh->method = HTTP_DELETE; - } else if (strstr(buffer, "HEAD") != NULL) { - rh->method = HTTP_HEAD; - } else if (strstr(buffer, "OPTIONS") != NULL) { - rh->method = HTTP_OPTIONS; - } else if (strstr(buffer, "PATCH") != NULL) { - rh->method = HTTP_PATCH; - } else if (strstr(buffer, "CONNECT") != NULL) { - rh->method = HTTP_CONNECT; - } else if (strstr(buffer, "CONNECT") != NULL) { - rh->method = HTTP_CONNECT; - } else { - rh->method = HTTP_UNKNOWN; - } +void HttpRequestHeaderFromS(HttpRequestHeader* rh, char* buffer, int bufferSize) { + + int bufferLen, wordIndex; // Find the bufferLen of the string bufferLen = strlen(buffer); - int start, end = 0; + int start = 0; + int end = 0; + // Initialize the tokenization state - char** saveTokenState; + char* saveTokenState = buffer; char* token; // Tokenize the request line - token = strtok_r(buffer, " ", saveTokenState); - while ((token = strtok_r(NULL, " ", saveTokenState)) != NULL) { - // Tokenize + while ((token = strtok_r(saveTokenState, "\n\r", &saveTokenState)) != NULL) { + printf("Token: %s\n", token); } // GET /path/to/file/ HTTP:1.1 + + +// int methodLen = 0; +// char methodStr[8] = ""; +// if (strstr(buffer, "GET") != NULL) { +// rh->method = HTTP_GET; +// } else if (strstr(buffer, "POST") != NULL) { +// rh->method = HTTP_POST; +// } else if (strstr(buffer, "PUT") != NULL) { +// rh->method = HTTP_PUT; +// } else if (strstr(buffer, "DELETE") != NULL) { +// rh->method = HTTP_DELETE; +// } else if (strstr(buffer, "HEAD") != NULL) { +// rh->method = HTTP_HEAD; +// } else if (strstr(buffer, "OPTIONS") != NULL) { +// rh->method = HTTP_OPTIONS; +// } else if (strstr(buffer, "PATCH") != NULL) { +// rh->method = HTTP_PATCH; +// } else if (strstr(buffer, "CONNECT") != NULL) { +// rh->method = HTTP_CONNECT; +// } else if (strstr(buffer, "CONNECT") != NULL) { +// rh->method = HTTP_CONNECT; +// } else { +// rh->method = HTTP_UNKNOWN; +// } + } diff --git a/source/http.h b/source/http.h index 468c8fe..e7c6feb 100644 --- a/source/http.h +++ b/source/http.h @@ -71,7 +71,7 @@ HttpResponseHeader* createHttpResponse(int statusCode,const char* contentType, void HttpResponseHeaderToS(HttpResponseHeader* rh, char* d, char* buffer, int bs); -void HttpRequestHeaderFromS(HttpRequestHeader* rh, char* buffer); +void HttpRequestHeaderFromS(HttpRequestHeader* rh, char* buffer, int bufferSize); int HttpCreate(); int HttpGetReqest(char* buffer); diff --git a/source/main.c b/source/main.c index a3d1c67..ed23c47 100644 --- a/source/main.c +++ b/source/main.c @@ -14,6 +14,7 @@ #include #include #include +#include #include @@ -25,19 +26,39 @@ void handleClient(int sock) { continue; } + sleep(1); HttpRequestHeader rh; - char packetBuilder[HTTP_RECIVE_PACKET_SIZE] = ""; + char packetBuilder[HTTP_RECIVE_PACKET_SIZE + 1] = ""; + int sizeOfBuffer = HTTP_RECIVE_PACKET_SIZE; + char* packetBuffer = malloc(HTTP_RECIVE_PACKET_SIZE); - int sizeOfPacket = recv(sock, packetBuilder, HTTP_RECIVE_PACKET_SIZE, 0); + int sizeOfPacket = 0;//= read(client_sock, packetBuilder, HTTP_RECIVE_PACKET_SIZE - 1); - char* packetBuffer = malloc(sizeOfPacket); - while ((sizeOfPacket = recv(sock, packetBuilder, HTTP_RECIVE_PACKET_SIZE, 0)) > 0) { - strncat(packetBuffer, packetBuilder, sizeOfPacket); + while ((sizeOfPacket = recv(client_sock, packetBuilder, + HTTP_RECIVE_PACKET_SIZE, 0)) > 0) { + printf("size of packet: %d\n", sizeOfPacket); + if(sizeOfPacket > HTTP_RECIVE_PACKET_SIZE) { + char* tmp = packetBuffer; + int tmpSize = sizeOfBuffer; + sizeOfBuffer += sizeOfPacket; + packetBuffer = malloc(sizeOfPacket); + memcpy(packetBuilder, tmp, tmpSize); + free(tmp); + memcpy(&packetBuffer[tmpSize], packetBuilder, sizeOfPacket); + continue; + } + memcpy(packetBuffer, packetBuilder, sizeOfPacket); + } + if(sizeOfPacket < 0) { + exit(-1); + } + printf("size of packet: %d\n", sizeOfPacket); - HttpRequestHeaderFromS(&rh, packetBuffer); + + HttpRequestHeaderFromS(&rh, packetBuffer, sizeOfPacket); free(packetBuffer); @@ -75,6 +96,11 @@ void handleClient(int sock) { } int main() { + char tmp[] = "testing this is a test"; + HttpRequestHeaderFromS(NULL,tmp, 23); + + + int sock = socket(AF_INET, SOCK_STREAM, 0); if (sock < 0) { perror("socket"); @@ -83,7 +109,7 @@ int main() { struct sockaddr_in addr; addr.sin_family = AF_INET; - addr.sin_port = htons(9090); + addr.sin_port = htons(9091); inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr); if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {