0

I can access perfectly to directories, but check50 doesn't agree:

$ check50 2015.fall.pset6.server2 server.c
:) server.c exists
:) server compiles
:) Requesting cat.jpg returns 200, image/jpeg, and correct image
:) Requesting cat.html returns 200, text/html, and correct file
:) 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 favicon.ico returns 200, image/x-icon, and correct file
:) Requesting test.css returns 200, text/css, and correct file
:) Requesting test.js returns 200, text/javascript, and correct file
:) Requesting hello.php returns 200, text/html, and correct output
:) Requesting hello.php? returns 200, text/html, and correct output
:) Requesting hello.php?name=Alice returns 200, text/html, and correct output
:) Requesting /test redirects to /test/
:( Requesting /test/ outputs /test/index.html
:( Requesting directory containing index.php outputs index.php
   \ expected output, but not "HTTP/1.1 200 OK\r\nContent-Type: text/h..."
:) Requesting two files in a row (cat.html then cat.jpg) succeeds
https://sandbox.cs50.net/checks/1c8d270424ed40e4a582f9d68b2e40f6

CL request:

$ curl -i http://localhost:8080/test/
HTTP/1.1 200 OK
Content-Type: text/html

<!DOCTYPE html>

<html>
    <head>
        <title>RickRoll'D</title>
    </head>
    <body>
        <iframe width="420" height="315" src="https://www.youtube.com/embed/oHg5SJYRHA0?autoplay=1&iv_load_policy=3" frameborder="0" allowfullscreen></iframe>
    </body>
</html>
~/workspace/pset6/ $ 

Server output:

$ ./server public
Using /home/ubuntu/workspace/pset6/public for server's root
Listening on port 8080
GET /test/ HTTP/1.1
HTTP/1.1 200 OK
~/workspace/pset6/ $ 

indexes:

bool indexes(char *path)
{
    bool rt = true;

    char *php = calloc(strlen(path) + strlen("index.php"), sizeof(char));
    strcpy(php, path);
    strcat(php, "index.php");

    char *html = calloc(strlen(path) + strlen("index.html"), sizeof(char));
    strcpy(html, path);
    strcat(html, "index.html");

    if (access(php, F_OK) != -1) {
        path = realloc(path, strlen(php) + 1);
        strcpy(path, php);
    } else if (access(html, F_OK) != -1) {
        path = realloc(path, strlen(html) + 1);
        strcpy(path, html);
    } else {
        rt = false;
    }
    free(php);
    free(html);
    return rt;
}

As I changed indexes, here is where I call it:

// use path/index.php or path/index.html, if present, instead of directory's path
bool index = indexes(path);
// list contents of directory
if (!index) {
    list(path);
    continue;
}

EDIT: Now use arrays instead of calloc:

indexes:

bool indexes(char *path)
{
    bool rt = true;

    char php[strlen(path) + strlen("index.php") + 1];
    strcpy(php, path);
    strcat(php, "index.php");

    char html[strlen(path) + strlen("index.html") + 1];
    strcpy(html, path);
    strcat(html, "index.php");

    if (access(php, F_OK) != -1) {
        path = realloc(path, strlen(php) + 1);
        strcpy(path, php);
    } else if (access(html, F_OK) != -1) {
        path = realloc(path, strlen(html) + 1);
        strcpy(path, html);
    } else {
        rt = false;
    }

    return rt;
}

check50:

$ check50 2015.fall.pset6.server2 server.c
:) server.c exists
:) server compiles
:) Requesting cat.jpg returns 200, image/jpeg, and correct image
:) Requesting cat.html returns 200, text/html, and correct file
:) 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 favicon.ico returns 200, image/x-icon, and correct file
:) Requesting test.css returns 200, text/css, and correct file
:) Requesting test.js returns 200, text/javascript, and correct file
:) Requesting hello.php returns 200, text/html, and correct output
:) Requesting hello.php? returns 200, text/html, and correct output
:) Requesting hello.php?name=Alice returns 200, text/html, and correct output
:) Requesting /test redirects to /test/
:( Requesting /test/ outputs /test/index.html
:) Requesting directory containing index.php outputs index.php
:) Requesting two files in a row (cat.html then cat.jpg) succeeds
https://sandbox.cs50.net/checks/f662b9275cd94a6a86b12504436d419d

SOLVED: I'll remember... Malloc is EVIL! And copy-paste sometimes, too.

1
  • I found that I was leaking memory, so I changed it to use arrays. That solved one of the check50 – alx Nov 11 '16 at 23:33
1

Your function now has index.php written two times.

One here: strcat(php, "index.php");

Another here: strcat(html, "index.php");

1
  • 1
    Oh, that one was very dumb!!! hehehe now check50 is happy, and I even more! – alx Nov 12 '16 at 1:18

You must log in to answer this question.

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