I am getting 501 Not implemented on every file i open. Via valgrind i get the error many times:
==4890== Conditional jump or move depends on uninitialised value(s)
==4890== at 0x4C2F1D0: strcmp (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4890== by 0x401EBC: lookup (server.c:708)
==4890== by 0x401C15: main (server.c:249)
==4890==
==4890== Conditional jump or move depends on uninitialised value(s)
==4890== at 0x401EC2: lookup (server.c:708)
==4890== by 0x401C15: main (server.c:249)
==4890==
Here are my parse and lookup functions:
const char* lookup(const char* path)
{
// TODO
char* c = strrchr(path, '.');
int n = strlen(c);
char a[n];
for (int i = 0; c[i]; i++) {
a[i] = tolower(c[i]);
}
if (strcmp(a, ".css") == 0) {
return "text/css";
}
if (strcmp(a, ".html") == 0) {
return "text/html";
}
if (strcmp(a, ".gif") == 0) {
return "image/gif";
}
if (strcmp(a, ".ico") == 0) {
return "image/x-icon";
}
if (strcmp(a, ".jpg") == 0) {
return "image/jpeg";
}
if (strcmp(a, ".js") == 0) {
return "text/javascript";
}
if (strcmp(a, ".php") == 0) {
return "text/x-php";
}
if (strcmp(a, ".png") == 0) {
return "image/png";
}
return NULL;
}
/**
* Parses a request-line, storing its absolute-path at abs_path
* and its query string at query, both of which are assumed
* to be at least of length LimitRequestLine + 1.
*/
bool parse(const char* line, char* abs_path, char* query)
{
// check if method is of type GET
if (strncmp(line, "GET ", 4) != 0)
{
error(405);
return false;
}
// check if request-target starts with '/'
if (line[4] != '/')
{
error(501);
return false;
}
// get request target
char* haystack = strchr(line, '/');
char* needle = strchr(haystack, ' ');
// will be used to check what comes after the request-target
char* http_version = needle;
// check for extra targets
char* errchk = strchr(http_version + 1, ' ');
if (errchk != NULL)
{
error(505);
return false;
}
char target[needle - haystack];
strncpy(target, haystack, needle - haystack);
target[needle - haystack] = '\0';
errchk = strchr(target, '\"');
if (errchk != NULL)
{
error(400);
return false;
}
// parse absolute path
haystack = target;
needle = strchr(haystack, '?');
// check if a query is present
if (needle == NULL)
{
strcpy(abs_path, haystack);
query[0] = '\0';
}
else
{
// if so, remove query
char path[needle - haystack + 1];
strncpy(path, haystack, needle - haystack);
path[needle - haystack + 1] = '\0';
strcpy(abs_path, path);
// parse query
strcpy(query, needle + 1);
}
// check for HTTP version
needle = strstr(http_version, "HTTP/1.1");
if (needle == NULL)
{
error(505);
return false;
}
return true;
}
Here are my load and indexes functions:
bool load(FILE* file, BYTE** content, size_t* length)
{
// allocate memory on the heap
*content = NULL;
*length = 0;
char* file_content = malloc(sizeof(char));
if (file_content == NULL)
{
return false;
}
int i = 0;
for (int c = fgetc(file); c != EOF; c = fgetc(file))
{
file_content[i] = (char) c;
i++;
file_content = (char*) realloc(file_content, sizeof(char)*(i+1));
}
// add terminating string and increase the size by one
// stores the address of the first byte of file contant in heap
*content = file_content;
*length = i;
return true;
}
char* indexes(const char* path)
{
int path_length = strlen(path) + 1;
char* ind_php = "/index.php";
char* ind_html = "/index.html";
char* inx1 = malloc(path_length + 12);
if (inx1 == NULL) return NULL;
strcpy(inx1, path);
strcat(inx1, ind_php);
if (access(inx1, F_OK) != -1) {
return inx1;
} else {
free(inx1);
}
char* inx = malloc(path_length + 12);
if (inx == NULL) return NULL;
strcpy(inx, path);
strcat(inx, ind_html);
if (access(inx, F_OK) != -1) {
return inx;
} else {
free(inx);
}
return NULL;
}
Any help will be really appreciated. PS : I checked my parse and lookup functions with check50 (server1.c) and got all green ticks.
Thanks in advance
==4890== by 0x401EBC: lookup (server.c:708)
. Review your lookup function for errors in your use of strcmp (==4890== at 0x4C2F1D0: strcmp ......
).