working on adding requests

This commit is contained in:
2025-10-09 13:41:21 -07:00
parent 5f058a01bf
commit f186358fc7
4 changed files with 69 additions and 38 deletions

Binary file not shown.

View File

@@ -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;
// }
}

View File

@@ -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);

View File

@@ -14,6 +14,7 @@
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h>
@@ -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) {