0

I'm almost done with parse function, but I get 301 Moved Permanently error in the above mentioned cases. I am aware that .exe is not present in our lookup function, but shouldn't we return NULL in that case? Also, I saw that staff wrote lines to handle these two cases. In my test file, everything works, so I really have no clue what I am doing wrong.

Any idea what might the problem be, please?

Thanks!

EDIT: As always, I didn't post my code. I kind of avoid it, although I am aware that you couldn't tell anything without it. :)

Here's my parse function:

bool parse(const char* line, char* abs_path, char* query)
{

char* first_space = strchr(line, ' ');
char* second_space = strrchr(line, ' ');
int l_line = strlen(line);
int l_method = l_line - strlen(first_space);
int l_request = l_line - l_method - strlen(second_space) - 1;

char request[l_request + 1];
char method[l_method + 1];

// copy request and method into its arrays
strncpy(request, first_space + 1, l_request);
strncpy(method, line, l_method);

request[l_request] = '\0';
method[l_method] = '\0';

// check for method
if (strcmp(method, "GET") != 0)
{
    error(405);
    return false;
}

// check for both spaces
if (first_space == NULL || second_space == NULL)
{
    error(405);
    return false;
}

// make sure the beggining of the request is ok
if (request[0] != '/')
{
    error(501);
    return false;
}

// we dun want no quotations marks
for (int i = 0, n = strlen(request); i <= n; i++)
{
    if (request[i] == '"')
    {
        error(400);
        return false;
    }
}

char* compare = strstr(line, "HTTP/1.1");
if (compare == 0)
{
    error(505);
    return false;
}

// makin' room for version
char* copy_version = malloc(sizeof(10));
strncpy(copy_version, compare, strlen(compare) - 2);

// query - what to do and not do if there's ? or not
char* q_mark = strchr(request, '?');
if (q_mark != NULL)
{
    if (strcmp(q_mark + 1, "") == 0)
    {
        query = malloc(sizeof(LimitRequestLine + 1));
        query[0] = '\0';
        abs_path = malloc(sizeof(LimitRequestLine + 1));
        strncpy(abs_path, request, strlen(request) + strlen(q_mark));

    }    

    else if (q_mark + 1 != NULL)   
    {
        query = malloc(sizeof(LimitRequestLine + 1));
        strcpy(query, q_mark);
        abs_path = malloc(sizeof(LimitRequestLine + 1));
        strncpy(abs_path, request, strlen(request) - strlen(query));
    }
}

else if (q_mark == NULL)
{
    query = malloc(sizeof(LimitRequestLine + 1));
    query[0] = '\0';
    abs_path = malloc(sizeof(LimitRequestLine + 1));
    strncpy(abs_path, request, strlen(request));
}

return true;
}

I would like to add that before I implemented query, I had all green smileys (except for the last one, which was "TODO"), and although I did not change a thing, later I got these three red ones. Now, when I run check50 server2, the only thing that's wrong is the last line - which means I did not implement indexes well, but I think I know how to do it properly. What bugs me, is the check50 server1.

2
  • mind updating your question with your code?
    – kzidane
    Commented Mar 13, 2016 at 0:49
  • @Kareem, done. I don't understand under which circumstances we get 301 Moved Permanently. And the rest is described above. Thanks for your time!
    – Lana
    Commented Mar 13, 2016 at 7:37

1 Answer 1

2

This is bad abs_path = malloc(sizeof(LimitRequestLine + 1)); (and query too). Remember, parse receives a pointer to abs_path, which is already allocated on the stack (implicitly) in main. The malloc gives it a new address on the heap. The strncpy updates the contents at that (new) address. But back in main it will be looking for abs_path at the "original" address, the contents of which have not been updated. If I'm understanding properly, it works sometimes (eg /cat.jpg) and not others (eg /cat.exe). Frankly, I'm a little confused by that, and I would say it is due to the vagaries of memory on the stack and the heap. You might consider adding some printfs in parse and after you return from parse. Something like

printf("contents....%s<-\n",abs_path);
printf("pointer  %p\n",abs_path);`

would illustrate.

12
  • I updated my question. It works now, as far as parse is concerned, so thank you a lot. Should I delete my parse function?
    – Lana
    Commented Mar 15, 2016 at 7:17
  • For the benefit of future readers, you should start a new question with your new question. The post can only have value to others if the answer goes with the question. Now that you've edited the parse code, the original answer is meaningless. I suggest you restore the original parse, and add another question for indexes. When you do, be sure to clarify "weird lines of code as an output". Commented Mar 15, 2016 at 13:19
  • Hey, I am truly sorry for bothering you, but could you just tell me why I've got "requesting two files in a row" red? It's marked todo and honestly, I've spent so much time on this that I haven't got a clue what to do with it anymore.
    – Lana
    Commented Mar 17, 2016 at 7:41
  • abs_path is not null terminated. Look closely at your strncpy(s). The simplest one strncpy(abs_path, request, strlen(request)); does not copy the null byte. Specifically, if request = "/cat.jpg". strlen(request) is 8. The null terminator is in the 9th byte, thus does not get copied to abs_path. Commented Mar 17, 2016 at 13:21
  • I used printf before seeing your comment, and in someplaces, the abs_path added "co" which I assume is from .ico extension. I added + 1 at the end of strncpy, but nothing changed.
    – Lana
    Commented Mar 17, 2016 at 13:49

You must log in to answer this question.

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