0

Hello, I've been working on Pset6, which requires implementation of four functions on previously writen code. Apparently, as by manually testing, it should pass every test on check50, but that's not the case once check50 runs.

These are the tests I'm failing:

:( Requesting cat2.HTML returns 200, text/html, and correct file

:( Requesting cat3.HtMl returns 200, text/html, and correct file

:( Requesting cat.gif returns 200, image/gif, and correct file

:( Requesting test.js returns 200, text/javascript, and correct file

:( Requesting hello.php? returns 200, text/html, and correct output \ expected output, not an exit code of 0

:( Requesting /test/ outputs /test/index.html

For the first two tests I created files with those names to check if the problem was with my code not properly handling capital letters on the file extension, that was not the case, files with capitals in their extensions were loaded properly. For the .gif image and .js javascript I did not run manual tests. Requesting hello.php? manually returns the exact same thing as requesting hello.php. I'm not sure what "expected output, but not an exit code of 0" means. Finally, manually requesting /test/ does indeed return /test/index.html which is actually just a video called RickRolle'D.

Here are my four functions:

Code removed

Any suggestions on why I might be failing these tests on the check50 app would be greatly appreciated.

6
  • bytes should (almost) always be 2 because you are always reading 2 bytes here fread(buffer, 2, 1, file);. Why, then, are you doing bytes * 2 in so many places? Also, just like all the other psets, this while ( !feof(file) ) is not the proper way to detect end-of-file. You do the one extra read thing and bugs ensue. I think you need to put in more time on load, then either edit the question appropriately, or better still, write the answer : ) This expected output, not an exit code of 0 generally indicates a seg fault. Test hello.php? with a curl request, because browsers drop the ?. Jul 30, 2016 at 22:02
  • I was under the impression that fread() returns 0 if unsuccesful, or 1 if the correct number of bytes was read, thats's precisely the problem I had when first implementing it. I tried to read 512 bytes at a time and store the result of fread in bytes which i then passed by itself as argument to realloc and memcpy. But then realized it was always returning 0 or 1. Mimo31 pointed that out in a previous question.
    – dLopez
    Jul 30, 2016 at 23:21
  • So so sorry, I was wrong. And personally, I think fread at 512 is the (greedy!) way to go. It will return the number of bytes (from man fread) "This number equals the number of bytes transferred only when size is 1." so fread(buffer,1,512,file) might do the trick. check50 could be failing on files with an odd length (hello.php is 281 bytes), because your length is always even. (Although seg fault is still indicated for /hello.php?). If this gets you back on track, be sure to write an answer. You'll be able to accept it in 2 days. Jul 31, 2016 at 1:00
  • I cant believe how I didn't see that just switching places between bytes per sector and sectors read would solve my problem, now I can effectively read 512 bytes at a time and return the true amount of bytes read. I just changed back to fread(buffer, 1, 512, file). I also thought my problem was based on the fact that only even number of bytes were read. Thanks a lot!!
    – dLopez
    Jul 31, 2016 at 2:09
  • The one test I'm still failing, and the part I didn't fully understand from your first comment, was when requesting /hello.php?. I understand now by looking at the request printed on my console that even if I manually type /hello.php? on the browser the request made to the server is actually /hello.php. But how can I work around this? Is this caused by my parse function?
    – dLopez
    Jul 31, 2016 at 2:16

1 Answer 1

1

The parse() function doesn't correctly handle a case where the request-line is comprised of only the absolute path ending with a question mark, it needs further error checking. When fixed the function should no longer return a non-zero value when /hello.php? is requested.

The load() function ONLY reads correctly sectors of two bytes, hence only correctly returning files which size in bytes is an even number, any other files would be returned but the final byte would be lost. Fixinig this should probably make the server pass the rest of the tests.

1
  • *** Thanks a lot to DinoCoderSaurus for his input!! ***
    – dLopez
    Jul 31, 2016 at 20:37

You must log in to answer this question.

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