Here are specific things to look at:
load Declare c as an int instead of size_t, since size_t is unsigned.
Your allocation for path2 is wrong
char* path2 = (char*)malloc(sizeof(strlen(php) + strlen(html) + 1)); First, the
sizeof has got to go. It will probably return 4, since the
argument will be an integer. Next, path2 is going to be either
Reread the specification for indexes in the instructions.
Complete the implementation of indexes in such a way that the function, given a /path/to/a/directory, returns /path/to/a/directory/index.php if index.php actually exists therein, or /path/to/a/directory/index.html if index.html actually exists therein, or NULL. In the first of those cases, this ...
If it exists, you simply have to return its path, as a string. For example, if the input is the string /gifs/cats, and there is indeed a file named index.html in the /gifs/cats directory, then you should return a string containing /gifs/cats/index.html.
Response to edit#1:
NB: The code post turned char* html = malloc(12); into this char html = malloc(12); which was only confusing for a little while :)
A major problem you are encountering is your use of strcat. I think you are encountering the segfault right here:
path1 = strcat((char*)path, php);
At least in the context of this class, you should ...
The function signature of strstr is
char *strstr(const char *haystack, const char *needle)
Since you have declared needle a const char*, you are violating the contract. I recommend further reading in the doc, specifically
These functions return a pointer to the beginning of the substring, or NULL if the substring is not found.
in the context ...
First, verify your directory structure is correct. From the instructions, when the distro code is unzipped your "tree" from the pset6 directory should look like this:
│ ├── cat.html
│ ├── cat.jpg
│ ├── favicon.ico
│ ├── hello.html
│ ├── hello.php
│ └── test
│ └── index.html
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 ...
There are problems in this index function. However, they are not causing all the check50 failures. Those problems probably stem from load function. Since check50 (probably) makes a direct request to cat.jpg (as with curl), server would not call indexes, thus it could not account for that failure.
The problem in the indexes function has to do with scope. ...
Your question is too extensive, I am not a person who likes to reprimand anyone, but the work of debugging the program has to leave you, the time required to check and respond to all errors is very extensive, and this back out to most people. That said I will try to give you the tools necessary to use GDB. At first it is necessary to use check50 1 and ...
You'll upload server.c to the staff's computer and the file will be tested there in their directories, not in your directories.
And they decided that the files will be called index.
If this answers your question, please click the check mark to accept.
buffer is allocated for one byte here BYTE* buffer = malloc(sizeof(BYTE));
This while(fread(buffer, 512, 1, file) == 1) reads 512 bytes into buffer.
And buffer is only and ever allocated for 1 byte; this *content = realloc(buffer, (len + 1) + sizeof(BYTE)); doesn't reallocate buffer. It reallocates the memory pointed to by buffer and stores the "new"...
Does valgrind return the leak if it finds index.html or index.php? I would predict this indexes would leak memory only if neither of those files is "found". Generally speaking, one would not change anything about "main" for this assignment, so adding a free to main is not the right approach. Since the memory for pathCopy is allocated in the indexes function, ...
The old sizeof trap. strlen(path) + strlen(php) + 1 returns an integer. sizeof an integer is 4. In two places. Do not use sizeof. Here is a good explanation.
Think about this, if you allocate dir for the larger string in the first place, there would be no need to free/malloc it again. That doesn't appear to be causing a problem (besides the sizeof problem ...
Here's what I do. Use debug50 instead of the command line gdb. If you haven't used that before:
Run update50 to be sure you are up to date (the IDE is on version 81 at the moment). Then, here's part of this year's class where Prof Malan explains how debug50 works. https://video.cs50.net/2016/fall/lectures/2?t=22m20s (if it doesn't go to that timestamp, ...
sizeof bad. char* temp_path = malloc(sizeof(path)); path is a pointer. sizeof a pointer is 4 or 8 depending on architecture. Looks like you want the length of the strings, which is a different function. Et. al.
Low hanging fruit (since you give no indication what problems you are experiencing).
Read the man page on access and pay special attention to the return value. Spoiler: it ain't a bool.
memcpy(&content, &temp, 1); is always copying temp into the first byte of content.
If you are going to treat array and array2 as string literals, why not declare them as such (eg char* array = "index.php"). No allocation muss, no null-terminator fuss.
gdb hint: inspect the value of index in main after you return from indexes.
The real problem comes here: char* ptr_best = try1; (and try2 too). You're setting a the pointer ptr_best the ...
gdb to the rescue!
Open two terminals.
In the first, call gdb ./server. There's a guide here. Of course, you will set a breakpoint at load instead of parse.
In the second terminal, cd to your document root directory
Send a curl request to your server. Something like this:
curl -i http://localhost:8080/hello.html. (Don't request an image file,
After a lot of help from DinoCoderSaurus, I have finally figured out what was wrong with my code.
First of all, I wasn't incrementing length properly in load.
Then I wasn't returning anything when there was no query.
Also, the load function had a problem. I was storing a lot of EOF's at the end of my string.
The most important problem was scope. A couple ...
Unexpected end of input
Indicates a timeout on the check50 server. It has not processed your request. The only advice is "try again later".
Assuming you mean the "non_existAnt" file test did not pass, this usually indicates some problem with your abs_path from the parse function. There is no "implementation" for error 404, it "comes with" the ...
first I wanna mention that it's really bad practice to have magical numbers (e.g., 10 and 11) in your code as they make your code difficult to understand.
second, you don't really need to check whether path ends with a '/'. you may assume that per the distribution code.
you should use access to check whether a file exists rather than unnecessarily fopening ...
Scope. Pointers. Look at the difference between the way you create abs_path and the way you create query. This strncpy(abs_path, reqtar, aplen); changes the contents of the memory that abs_path points to, while this query = strchr(reqtar, '?'); changes the value of the pointer. When function returns to main, it is pointing to the same chunk of memory as when ...
EDIT Sorry again for the misdirect. The elusive 1 eludes me at least once a day. This strncpy(storage, buffer, count); is causing a problem. Content can be anything. And content can have bytes with value of 0 (which is what the null-terminator is). (The 5th byte of cat.jpg is one such). In most cases (all cases?) content is not a c string. The strncpy ...
How many bytes have you allocated to pathcopy? How many bytes are you trying to stuff in it? (Ditto pathcopy2). Once you fix that, make sure newpath is sized properly.
Also here's more trouble your way:
char* newpath = malloc(sizeof(pathcopy)); You say: give me a char pointer to a chunk of memory the size of pathcopy (which, by the way, isn't really ...
according to the specs, you should first check for index.php's existence and return a path to it, if it exists. then check for index.html's existence, and return a path to it, if it exists. otherwise, you just return NULL.
see http://cdn.cs50.net/2016/x/psets/6/pset6/pset6.html#indexes_2 for more!