153 lines
3.3 KiB
C
153 lines
3.3 KiB
C
#include <stdlib.h>
|
|
|
|
#include "debug.h"
|
|
#include "http.h"
|
|
#include "util.h"
|
|
|
|
/* The length of the buffer to read the stuff from the socket into. */
|
|
#define BUFFER_LENGTH 256
|
|
|
|
char* http_read_header(socket_t csocket)
|
|
{
|
|
char* result = NULL;
|
|
char buffer[BUFFER_LENGTH];
|
|
int bytes_received = 0;
|
|
|
|
DEBUG_PRINTF(("[HTTP] Reading header.\n"));
|
|
do
|
|
{
|
|
bytes_received = recv(csocket, buffer, BUFFER_LENGTH - 1, 0);
|
|
if (bytes_received > 0)
|
|
{
|
|
char* substr = NULL;
|
|
|
|
buffer[bytes_received] = '\0';
|
|
|
|
/* Append buffer to result: */
|
|
result = strcatd(result, buffer);
|
|
|
|
/* See if result now has two CRLFs in a row, to indicate the
|
|
end of the header: */
|
|
substr = strstr(result, "\r\n\r\n");
|
|
if (substr)
|
|
{
|
|
substr[4] = '\0';
|
|
break;
|
|
}
|
|
}
|
|
} while (bytes_received > 0);
|
|
|
|
DEBUG_PRINTF(("[HTTP] Got header:\n%s\n[HTTP] End header.\n", result));
|
|
|
|
return result;
|
|
}
|
|
|
|
char* http_get_requesturi(const char* header)
|
|
{
|
|
char* start = ((char*) strchr(header, ' ')) + 1;
|
|
if (start == NULL)
|
|
{
|
|
return NULL;
|
|
}
|
|
else
|
|
{
|
|
const char* stop = strchr(start, ' ');
|
|
if (stop == NULL)
|
|
{
|
|
return NULL;
|
|
}
|
|
else
|
|
{
|
|
/* Count the length of the request: */
|
|
char* ptr = NULL;
|
|
size_t count = 0;
|
|
for (ptr = start; ptr != stop; ptr++)
|
|
{
|
|
count ++;
|
|
}
|
|
|
|
return strncatd(NULL, start, count);
|
|
}
|
|
}
|
|
}
|
|
|
|
const char* http_get_method(const char* header)
|
|
{
|
|
/* If header starts with GET, then we have a GET request. */
|
|
if (strstr(header, HTTP_METHOD_GET) == header)
|
|
{
|
|
return HTTP_METHOD_GET;
|
|
}
|
|
else
|
|
{
|
|
return NULL;
|
|
}
|
|
}
|
|
|
|
void http_send_canned_response(socket_t socket, int status)
|
|
{
|
|
static const char* html1 = "<html>HTTP Status code ";
|
|
static const char* html2 = "</html>";
|
|
|
|
char buffer[8];
|
|
sprintf(buffer, "%d", status);
|
|
|
|
/* Send the appropriate header: */
|
|
http_send_header(socket, status);
|
|
|
|
/* Send some HTML: */
|
|
net_send_string(socket, html1, strlen(html1));
|
|
net_send_string(socket, buffer, strlen(buffer));
|
|
net_send_string(socket, html2, strlen(html2));
|
|
}
|
|
|
|
void http_send_header(socket_t socket, int status)
|
|
{
|
|
char buffer[32];
|
|
char* header = NULL;
|
|
|
|
/*
|
|
static struct utsname *our_name = NULL;
|
|
if (uname(our_name))
|
|
{
|
|
return NULL;
|
|
}
|
|
our_name->nodename
|
|
*/
|
|
|
|
/* Make status line: */
|
|
sprintf(buffer, "HTTP/1.1 %d %d\r\n", status, status);
|
|
header = strcatd(header, buffer);
|
|
|
|
/* Add various other headers: */
|
|
header = strcatd(header, "Content-Type: text/html\r\n");
|
|
header = strcatd(header, "Server: CS4210P1/vlad,omar\r\n");
|
|
header = strcatd(header, "Connection: Close\r\n");
|
|
header = strcatd(header, "\r\n");
|
|
|
|
net_send_string(socket, header, strlen(header));
|
|
|
|
free(header);
|
|
}
|
|
|
|
void http_send_text_file(socket_t socket, FILE* f)
|
|
{
|
|
char buffer[BUFFER_LENGTH];
|
|
size_t bytes_read = 0;
|
|
|
|
while (!feof(f))
|
|
{
|
|
bytes_read = fread(buffer, sizeof(char), BUFFER_LENGTH - 1, f);
|
|
buffer[bytes_read] = '\0';
|
|
|
|
if (bytes_read > 0)
|
|
{
|
|
net_send_string(socket, buffer, bytes_read);
|
|
}
|
|
else
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
}
|