working on adding requests
This commit is contained in:
BIN
DesignedWorlds
BIN
DesignedWorlds
Binary file not shown.
@@ -94,51 +94,56 @@ void HttpResponseHeaderToS(HttpResponseHeader* rh,char* d, char* buffer, int buf
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void HttpRequestHeaderFromS(HttpRequestHeader* rh, char* buffer) {
|
void HttpRequestHeaderFromS(HttpRequestHeader* rh, char* buffer, int bufferSize) {
|
||||||
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
int bufferLen, wordIndex;
|
int bufferLen, wordIndex;
|
||||||
|
|
||||||
// Find the bufferLen of the string
|
// Find the bufferLen of the string
|
||||||
bufferLen = strlen(buffer);
|
bufferLen = strlen(buffer);
|
||||||
int start, end = 0;
|
int start = 0;
|
||||||
|
int end = 0;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Initialize the tokenization state
|
// Initialize the tokenization state
|
||||||
char** saveTokenState;
|
char* saveTokenState = buffer;
|
||||||
char* token;
|
char* token;
|
||||||
|
|
||||||
// Tokenize the request line
|
// Tokenize the request line
|
||||||
token = strtok_r(buffer, " ", saveTokenState);
|
while ((token = strtok_r(saveTokenState, "\n\r", &saveTokenState)) != NULL) {
|
||||||
while ((token = strtok_r(NULL, " ", saveTokenState)) != NULL) {
|
printf("Token: %s\n", token);
|
||||||
// Tokenize
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GET /path/to/file/ HTTP:1.1
|
// 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;
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ HttpResponseHeader* createHttpResponse(int statusCode,const char* contentType,
|
|||||||
|
|
||||||
|
|
||||||
void HttpResponseHeaderToS(HttpResponseHeader* rh, char* d, char* buffer, int bs);
|
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 HttpCreate();
|
||||||
int HttpGetReqest(char* buffer);
|
int HttpGetReqest(char* buffer);
|
||||||
|
|||||||
@@ -14,6 +14,7 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
|
#include <unistd.h>
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
|
|
||||||
|
|
||||||
@@ -25,19 +26,39 @@ void handleClient(int sock) {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sleep(1);
|
||||||
HttpRequestHeader rh;
|
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) {
|
while ((sizeOfPacket = recv(client_sock, packetBuilder,
|
||||||
strncat(packetBuffer, packetBuilder, sizeOfPacket);
|
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);
|
free(packetBuffer);
|
||||||
|
|
||||||
@@ -75,6 +96,11 @@ void handleClient(int sock) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
char tmp[] = "testing this is a test";
|
||||||
|
HttpRequestHeaderFromS(NULL,tmp, 23);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int sock = socket(AF_INET, SOCK_STREAM, 0);
|
int sock = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
if (sock < 0) {
|
if (sock < 0) {
|
||||||
perror("socket");
|
perror("socket");
|
||||||
@@ -83,7 +109,7 @@ int main() {
|
|||||||
|
|
||||||
struct sockaddr_in addr;
|
struct sockaddr_in addr;
|
||||||
addr.sin_family = AF_INET;
|
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);
|
inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr);
|
||||||
|
|
||||||
if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
|
if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
|
||||||
|
|||||||
Reference in New Issue
Block a user