0

When I try to run server.c, it's returning the 505 error, which is very confusing to me. I built the code separating everything out in a separate .c file to make sure everything works correctly, and I used a couple test request-lines, and those spit out the correct HTTP version HTTP/1.1 without any trouble, but now I'm seeing this error.

For completeness, here is all of my code from validate request-line to extract path's extension just in case there is a glaring error somewhere else:

//validate request-line

        //check if request-line matches template, else return error(400)

        if(strchr(line, ' ') == NULL)
        printf("error(400)");

        //future indexes of spaces inside line[]
        int spaceOne = 0; 
        int spaceTwo = 0;
        int clfloc = 0;
        //find index of first space
        while(line[spaceOne] != ' ')
        {
            spaceOne++; 
        }
        spaceTwo = spaceOne + 1;
        //find index of second space
        while(line[spaceTwo] != ' ')
        {
            spaceTwo++;
        }
        clfloc = spaceTwo + 1;
        //find index of CRLF
        while(line[clfloc] != '\r')
        {
            clfloc++;
        }

        //separate out method
        int metLen = (spaceOne + 1);
        char method[metLen];
        for(int i = 0; i < spaceOne; i++)
        {
            method[i] = line[i];
        }
        method[metLen - 1] = '\0';

        //separate out request-target
        int rTarLen = (spaceTwo - spaceOne);
        char requestTarget[rTarLen];
        //separate counter variable to initialize to zero
        int count = spaceOne + 1;
        for(int i = 0; i < rTarLen; i++)
        {
            requestTarget[i] = line[count];
            count++;
        }
        requestTarget[rTarLen - 1] = '\0';

        //separate out HTTP-version
        int HTTPvLen = (clfloc - spaceTwo) + 1;
        char HTTPv[HTTPvLen];
        count = 0;
        for(int i =spaceTwo + 1; i < clfloc; i++)
        {
            HTTPv[count] = line[i];
            count++;
        }
            HTTPv[HTTPvLen - 2] = '\r';
            HTTPv[HTTPvLen - 1] = '\0';

        //separate out absolute-path
        int sSqBr = 0;
        if(strstr(line, "[") != NULL)
        {
            while(requestTarget[sSqBr] != '[')
            {
                sSqBr++;
            }
        }
        else
            sSqBr = strlen(requestTarget);
        int abPathLen = sSqBr + 1;
        char abPath[abPathLen];
        for(int i = 0; i < sSqBr; i++)
        {
            abPath[i] = requestTarget[i];
        }
            abPath[abPathLen - 1] = '\0';

        //various errors returned
        if(method == NULL || strcmp(method, "GET") != 0)
        {
            error(405);
            continue;
        }
        else if(requestTarget == NULL || requestTarget[0] != '/')
        {
            error(501);
            continue;
        }
        else if(strstr(HTTPv, "HTTP/1.1") != 0)
        {
            error(505);
            continue;
        }
        else if(strchr(abPath, '.') == NULL)
        {
            error(501);
            continue;
        }
        for(int i = 0; i < rTarLen; i++)
        {
            if(requestTarget[i] == '"')
            {
                error(400);
                continue;
            }
        }

        //extract query from request-target   
        int eQMrk = sSqBr + 2;
        count = eQMrk;
        int queryLen = 0;
        if(strstr(line, "]") != NULL)
        {
            while(requestTarget[count] != ']')
            {
                count++;
                queryLen++;
            }  
        queryLen++;
        }
        else
            queryLen = 1;
        char query[queryLen];
        if(queryLen != 1)
        {
            count = 0;
            for(int i = eQMrk; i < ((eQMrk + queryLen) - 1); i++)
            {
                query[count] = requestTarget[i];
                count++;
            }
            query[queryLen - 1] = '\0';
        }
        else
            query[0] = '\0';


        //concatenate root and absolute-path
        char root[5] = {"root\0"};
        int pathLen = strlen(root) + strlen(abPath) + 1;
        char path[pathLen];
        strcpy(path, root);
        count = 0;
        for(int i = strlen(root); i < pathLen - 1; i++)
        {
            path[i] = abPath[count];
            count++;
        }
        path[pathLen - 1] = '\0';



        //ensure path exists
        if(access(path, F_OK) != 0)
            error(404);

        //ensure path is readable
        if(access(path, R_OK) != 0)
            error(403);

        //extract path's extension
        int lPer = strlen(path);
        while(path[lPer] != '.')
        {
            lPer--;
        }
        int extLen = strlen(path) - lPer;
        char extension[extLen];
        count=0;
        for(int i = lPer+1; i < strlen(path); i++)
        {
            extension[count] = path[i];
            count++;
        }
        extension[extLen] = '\0';

As always, thank you in advance!!!

0

I can see you are extracting te absolute path searching for "[" which you won't find because in the spec the [ are only there to show you the query is an optional statement. Instead you should be searching for "?".

The request from the web client may look like this:

GET /hello.php?q=Alice HTTP/1.1
  • I found that was just ONE of my many problems, but it definitely helped! – Chris Oct 21 '15 at 14:30

You must log in to answer this question.

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