Skip to content
Snippets Groups Projects
Commit de890bf2 authored by Abhisha Nirmalathas's avatar Abhisha Nirmalathas
Browse files

merge c onflict

parents ee84e6f2 52b8d9a2
No related branches found
No related tags found
No related merge requests found
......@@ -65,6 +65,54 @@ bool get_request(char* buff, int sockfd, char* file_name){
return true;
}
bool post_request(char *buff, int sockfd, char* file_name){
// locate the username, it is safe to do so in this sample code, but usually the result is expected to be
// copied to another buffer using strcpy or strncpy to ensure that it will not be overwritten.
char * username = strstr(buff, "user=") + 5;
int username_length = strlen(username);
// the length needs to include the ", " before the username
long added_length = username_length + 2;
// get the size of the file
struct stat st;
stat(file_name, &st);
// increase file size to accommodate the username
long size = st.st_size + added_length;
int n = sprintf(buff, HTTP_200_FORMAT, size);
// send the header first
if (write(sockfd, buff, n) < 0)
{
perror("write");
return false;
}
// read the content of the HTML file
int filefd = open(file_name, O_RDONLY);
n = read(filefd, buff, 2048);
if (n < 0)
{
perror("read");
close(filefd);
return false;
}
close(filefd);
// move the trailing part backward
int p1, p2;
for (p1 = size - 1, p2 = p1 - added_length; p1 >= size - 25; --p1, --p2)
buff[p1] = buff[p2];
++p2;
// put the separator
buff[p2++] = ',';
buff[p2++] = ' ';
// copy the username
strncpy(buff + p2, username, username_length);
if (write(sockfd, buff, size) < 0)
{
perror("write");
return false;
}
return true;
}
static bool handle_http_request(int sockfd)
{
// try to read the request
......@@ -105,8 +153,16 @@ static bool handle_http_request(int sockfd)
// sanitise the URI
while (*curr == '.' || *curr == '/')
++curr;
printf("**************THE CURR IS %s\n\n\n", curr);
if (strncmp(curr, "?start=Start", 12) == 0){
printf("matches start");
if (method == GET){
get_request(buff,sockfd, "3_first_turn.html");
}
}
// assume the only valid request URI is "/" but it can be modified to accept more files
if (*curr == ' ')
else if (*curr == ' ')
if (method == GET)
{
if (strncmp(curr, "start=Start", 11) == 0)
......@@ -118,50 +174,7 @@ static bool handle_http_request(int sockfd)
}
else if (method == POST)
{
// locate the username, it is safe to do so in this sample code, but usually the result is expected to be
// copied to another buffer using strcpy or strncpy to ensure that it will not be overwritten.
char * username = strstr(buff, "user=") + 5;
int username_length = strlen(username);
// the length needs to include the ", " before the username
long added_length = username_length + 2;
// get the size of the file
struct stat st;
stat("2_start.html", &st);
// increase file size to accommodate the username
long size = st.st_size + added_length;
n = sprintf(buff, HTTP_200_FORMAT, size);
// send the header first
if (write(sockfd, buff, n) < 0)
{
perror("write");
return false;
}
// read the content of the HTML file
int filefd = open("2_start.html", O_RDONLY);
n = read(filefd, buff, 2048);
if (n < 0)
{
perror("read");
close(filefd);
return false;
}
close(filefd);
// move the trailing part backward
int p1, p2;
for (p1 = size - 1, p2 = p1 - added_length; p1 >= size - 25; --p1, --p2)
buff[p1] = buff[p2];
++p2;
// put the separator
buff[p2++] = ',';
buff[p2++] = ' ';
// copy the username
strncpy(buff + p2, username, username_length);
if (write(sockfd, buff, size) < 0)
{
perror("write");
return false;
}
post_request(buff,sockfd, "2_start.html");
}
else
// never used, just for completeness
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment