It's a common problem when abs_path and query are built one character at a time. The problem seems to be in parse. abs_path (and query) are not properly null-terminated. So server returns a 404 error at line 209, before it executes lookup. See the link mentioned in the comment for hints on testing.
NB this is not a duplicate of the referenced call. That ...
You can refactor your code to use only one if statement!
Think about the number of MIME types you have to check for, and the number of matching file extensions.
Since there are the same number of each, you can store them in a pair of matching numerically-indexed arrays, so that the same array index will return a matching MIME type and extension.
In your ...
Well, here is the thing: I don't think you really have to allocate memory on the heap at all. Besides, you are never making use of the allocated memory and it is not recommended per the pset specification page.
Let's see how you are never making use of it by looking at a relatively simpler example
int i = 10;
i = 20;
The first statement creates an int ...
The "weird" results you report
For some reason, if I alter the order in which the file extensions are searched, cat.html suddenly doesn't load.
is the "unpredictable results" that happen when there are memory allocation problems and buffer overruns.
There shouldn't be an need for allocating in lookup(). You should simply be returning the string ...
Each dict in portfolio contains 3 keys as returned from the execute. There is no key current_price, so the html does not render anything for that column. If the key gets created in the for loop in index, then it will be available to the html. Something like record['current_price'] = share['price'].
A few things. Valid indexes would probably range from 0 to size-1, and as you chose your interval boundaries as both being part of the interval, your initial interval end should include the -1, to avoid accessing a word after the list, where it can't be.
Do not call strcmp up to three times, but only once, remembering the result in some variable, and check ...
One problem is here <form action="/quote.html" method="post">. There is not a quote.html in document root (it is in subdir templates), thus the 404. But do you really want the form action to be the html? Shouldn't it be the app route?
dictionary contains an array of words and each of those words contain an array of letters. So dictionary.words.letters would represent the first word in the dictionary.
You're on the right track, but it looks like you are trying to access the first character in the word? I'm not sure that is going to give you what you need, because what happens if two ...
When you use that keyword const, you're promising your compiler that you won't change the contents of what's inside the array. That's why the compiler yells... because you're trying to change it here identify[num] = path[letter]; and here identify[num] = '\0';
If you want to alter the contents of that array, you should declare it as a char array, not a ...
char* s = strrchr(path, '.');
as I understand strrchr returns a pointer to point '.', hence the string s would be something like ".css" for example, on the other hand strcmp, compares two string, to return zero, both chains must be identical, including uppercase or lowercase characters, you might want to take a look at the function strcasecmp
Since there are only two places in server.c that return a 505, and they are both in this parse function, that's a good place to start looking.
debug50 or gdb (tips here) would be perfect tools to help you find the problem. It takes some practice to use the effectively, but either will server you well as you work your way through this problem set.
First of all, WOW: your code is awesome.
I took LOOKUP on the side and I could not see any obvious errors.
I did find some behaviors on PARSE you might find useful.
This is how I debugged your function:
I have this program called parse.c where I copy and pasted your function:
There are some problems that are quick to see. They are most likely not all the problems in play here.
The server1 failure probably stems from here strncpy(request, req_pos, (v_pos - req_pos - 1)); From man strncpy:
The strncpy() function is similar, except that at most n bytes of src are copied. Warning: If there is no null byte
Since those are the only two tests that fail check50, redirect your attention to how query is built in parse. Since hello.php "works fine", verify that query is properly null-terminated. A helpful format string for printf debugging is "%s<-" so your eye is not fooled by non-printing characters.
Where are you printing "Type is:"? I don't think it's ...
strcasecmp is meant for comparing string without caring about capitalization. Comparing twice, with different capitalizations is not really useful here.
The reason why you are getting the 505 HTTP Version Not Supported error is because http_version starts with a whitespace.
Indeed, when doing char* needle = strchr(haystack, ' '); the ' ' char is included ...
There's only one place that server returns a 505, and that is from your parse function. Your server is never even getting to lookup. This guy if (strcmp(word, "HTTP/1.1") != 0) is always evaluating to false. Troubleshooting this would be a great job for gdb. Find some tips here.
Recall from the spec what a request line looks like:
method SP request-...
First suggest you send a request to server from browser or curl. You will discover that server seg faults. (That's what this \ expected output, not an exit code of 0 feedback from check50 indicates).
Next, in Week4 > Section > Dynamic Memory Allocation review the "Three Golden Rules" of memory allocation, around 8:45.
Then review your program for "...
for (char read = fgetc(file); read != EOF; read = fgetc(file)). read cannot be a char. Because of internal casting in fgetc, this can return a "false eof", and almost always does on binary (image) files. Some char in the image files is being cast to the int -1. EOF is defined (in stdio.h) as -1. The loop ends prematurely. If you cast c as an int (instead of ...
server should return 505 in exactly one place, from the parse function http version check. That means that strcmp(HTTP_version, "HTTP/1.1") does not return 0. That means that HTTP_version is not "HTTP/1.1". You could use gdb to determine why. At the least, you could add a printf before the test to see what the value of HTTP_version is. A suggestion is using ...
Hint: This line in indexes if (access(phpcheck, F_OK)) (and the other access) is wrong.
Suggestion: Read man access. Pay special attention to the RETURN section. access returns an int. Treat it like an int.
Hint: The pattern of errors from check50 makes load() the likely culprit.
Suggestion: Since the browser is giving "acceptable" output, look for ...
Program received signal SIGSEGV, Segmentation fault.
__strcpy_ssse3 () at ../sysdeps/i386/i686/multiarch/strcpy-ssse3.S:84
I would look for another way, for example:
// copy line to new string
char* temp = malloc(strlen(line) + 1);
if(temp == NULL)
All things being equal, the only place that server returns a 505 is the parse function. If this parse function reaches for(int i=0;i<strlen(line);i++), it will always return a 505. Howzat? Assuming a correct HTTP version, when you reach the second space, f == 2, this line if(line[i+1]!='H'||....
is true. What happens next? i is incremented, line[i]...