0

I've made some progress on server.c going through the code with gdb but now I'm stuck. It seems to be working (all pages and images load "correctly" in browser) except php files. It also does not pass any of the cs50 checks (except test redirects, the folder loads hello.php, and hello.php load correctly).

Parse function was my first thought but seems to be working:

783             if(strchr(absolute_path, '?') == NULL)
(gdb) p pathcopy
$1 = 0x609970 "/hello.php?name=Brandon"
(gdb) n
789             else if(strcmp(strchr(absolute_path, '?') + 1, "\0") == 0)
(gdb) 
798                 query = strchr(absolute_path, '?') + 1;
(gdb) 
799                 abs_path = strtok(pathcopy,"?");
(gdb) 
802             return true;
(gdb) p query
$2 = 0x60796f "?name=Brandon"
(gdb) p abs_path
$3 = 0x609970 "/hello.php"
(gdb) n
805     }

Since the browser appears correct, I assume it must be a problem with a null terminator or load. Since php also doesn't work, I'm leaning more toward how the file is loading.

Before I could use GDB and find where an error was thrown, or I knew what variables I wanted to check (eg. abs_path and query). Now my problem is my program isn't getting an error, and I don't know what variables to check (or what they should even be in the case of load).

Any guidance on where my problem might be or how to use GDB at this point would be appreciated.

Here is my code...

Update: I removed the code since it is a nearly complete working solution. Most of my errors came from small misunderstandings in how my code was operating. These errors were discovered using GDB to check variables with some of the tips DinoCoderSaurus posted below.

Another issue was how variables were transferring to different functions. Checking how the variables were reporting in different functions (using GDB) hinted that memory allocation/pointers were the issue.

In my case, these "memory" issues were fixed by mallocing sectors of memory to manipulate variables and then copying the finalized strings into the sector of memory called by the function (eg. malloc querycopy and then use strtok and other functions to manipulate it, and then transfer the result using strcpy(query,querycopy). Pointers still confuse me some, but any time I've found variable working in one function but not carrying over to the other function, pointers/memory was the issue.

May not be the best explanations but hopefully gives some insight if someone is facing similar problems.

1

php doesn't work at least in part because '?' should not be part of query. From the spec absolute-path [ "?" query ].

The problem with the load pasted here is it is an indexes function.

gdb advice for load:

  1. Use a curl request for cat.html or hello.html. Use curl so you don't have to bother with the pesky favicon.ico. Use cat.html or hello.html because they are a) small and b) human-readable.

  2. You can find out what *length should be by seeing how many bytes the file is on disk. Use ls -al *.html in your public directory. Output will be something like:

    -rw-rw-r-- 1 ubuntu ubuntu 156 Oct 24 2015 cat.html -rw-rw-r-- 1 ubuntu ubuntu 316 Oct 24 2015 hello.html

    The sizes are 156 and 316 respectively.

  3. Since both of those files are "human-readable", *content will look pretty much like the file does in an editor.

Addendum

In general, don't forget to test variables when you return to main. If they are created in memory local to a function, they may be "different" when you return.

That is the case with query. In this branch

else
        {

            query = strchr(absolute_path, '?');
            abs_path = strtok(pathcopy,"?");
        }

query (and abs_path) are pointers to locally allocated memory which will be "destroyed" when you return to main.

4
  • Whoops on the load function -- I accidentally "find and replaced" indexes with load. I thought it would be a nightmare to sort out, but I think I got it all fixed. Just must have copied that before I realized it. It's replaced with my load function now Sep 12 '16 at 0:46
  • Thanks... that pointed me in the right direction and my code now nearly passes 100%. The only problem is with .php? and .php?name=Alice. When I use a curl request and GDB I get the query variable being 'name=Alice' which should be correct? But it also doesn't work in the browser, so I'm missing something. Sep 12 '16 at 3:12
  • Answer edited.... Sep 12 '16 at 12:11
  • Finally got it! Thanks for the help! I removed the code (since it was nearly complete) and replaced it with my best explanation of how it was fixed. Sep 12 '16 at 17:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .