Skip to content

Commit

Permalink
[WIP] HTTP Post Request function done
Browse files Browse the repository at this point in the history
  • Loading branch information
S0S4 committed Sep 4, 2024
1 parent c360265 commit c6cd536
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/client/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ all:
$(CC) $(SRC) -o $(OUT) $(FLAGS)

debug:
$(CC) $(SRC) -o $(OUT) $(FLAGS) -g
$(CC) $(SRC) -o $(OUT) $(FLAGS) -g -m32

clean:
rm $(OUT)
Binary file removed src/client/client
Binary file not shown.
47 changes: 43 additions & 4 deletions src/client/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,54 @@
const char *CONTENT_LENGTH = "Content-Length: ";
const char *GET_REQ_TEMPLATE =
"GET %s HTTP/1.1\r\nConnection: keep-alive\r\n\r\n";

const char *POST_REQ_TEMPLATE =
"POST %s HTTP/1.1\r\nHost: %s\r\nContent-Type: %s\r\nContent-Length: %d\r\n%s\r\n\r\n";
/* Log a http_res_t */
void print_http_res(const http_res_t res) {
printf("--[ STATUS CODE: %i ]--\n", res.status_code);
printf("--[ REQUEST ]--\n%s\n--[ REQUEST ]--\n", res.request);
printf("--[ REQUEST ]--\n%s\n--[ RESPONSE ]--\n", res.request);
printf("%s\n", res.data);
puts("--[ END ]--");
}

int http_post(int sfd,const char* path,const char *host,const char *content_type, const char* parameters){

char buffer[HTTP_BUFFER_SIZE];
char *http_str;
ssize_t req;
buffer[HTTP_BUFFER_SIZE -1 ] = '\0';

snprintf(buffer,1023,POST_REQ_TEMPLATE,path,host,content_type,strlen(parameters),parameters);

req = send_request(sfd,buffer);
if(req < 0){
return HTTP_SOCKET_ERR;
}
printf("\n---[REQUEST]---\n%s", buffer);
ssize_t recv_bytes = recv_response(sfd,buffer,sizeof(buffer));

puts("[!] Receiving POST response:");
if(recv_bytes < 0){
return HTTP_INVALID_RESPONSE;
}

printf("\n---[RESPONSE]---\n%s\n", buffer);

// CHECK HTTP RESPONSE CODE

http_str = strtok(buffer, "\r\n");
http_str = strstr(http_str, " ");
http_str = strtok(http_str, " ");

int http_code = atoi(http_str);
if(http_code != 200){
puts("Invalid HTTP Code");
return -1;
}

return HTTP_SUCCESS;
}

int http_get(int sfd, const char *path, http_res_t *res) {
char request_buf[HTTP_BUFFER_SIZE]; // use separate buffer for the request
char buf[HTTP_BUFFER_SIZE];
Expand All @@ -36,7 +75,7 @@ int http_get(int sfd, const char *path, http_res_t *res) {
// send request
snprintf(request_buf, HTTP_BUFFER_SIZE-1, GET_REQ_TEMPLATE, path);
send_request(sfd, request_buf);

res->request = request_buf;
if (HTTP_VERBOSE) puts("Sent GET request");

Expand Down Expand Up @@ -110,7 +149,7 @@ int http_download_data_to_file(int sfd, const char *path, const char *f_path) {
perror("Error: Failed to write data to file");
fclose(file);
http_free(&res);
return -2;
return -2;
}

if (fclose(file) != 0) {
Expand Down
2 changes: 2 additions & 0 deletions src/client/http.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ typedef struct {
int http_get(int sfd, const char *path, http_res_t *res);
void http_free(http_res_t *res);
int http_download_data_to_file(int sfd, const char *path, const char *f_path);
int http_post(int sfd,const char* path,const char *host,const char *content_type, const char* parameters);
int download_files(int sfd, char *filename);
#endif
5 changes: 2 additions & 3 deletions src/client/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
// if (error != HTTP_SUCCESS) {
// return error;
// }



// http_free(&res);
Expand Down Expand Up @@ -45,8 +45,7 @@ int main() {
return EXIT_FAILURE;
}

http_download_data_to_file(sfd, "/", "resp.txt");

http_post(sfd,hostname,"/","text/plain","isDownload=yes");
close(sfd);
freeaddrinfo(ainfo);
return EXIT_SUCCESS;
Expand Down

0 comments on commit c6cd536

Please sign in to comment.