0

Hey friends thakyou for your help in advance I am having problem with my parse and lookup function. check50 is returning all fine except this :disappointed: Requesting cat.exe returns error code 501 \ expected output, but not "HTTP/1.1 404 Not Found\r\nContent-Type:..."

Can you please help me out. when i checked my code myself i found that it is not returning anything thats in the directory just throwin 404 code file not found even if the file is in directory PLease help me out guys.

This is my code

bool load(FILE* file, BYTE** content, size_t* length) {

        char* forcount = malloc(10 * sizeof(char));
        *length = 0;
        int count = 0;
        while(fread(forcount,1,10,file) != EOF)
        {

            count = count + 10;
            forcount = realloc(forcount,(count + 10) * sizeof(char));
            if(forcount == NULL)
            {
                return false;
            }
        }


        *length = count;

        *content = forcount;
        return true;
    }
    /**
     * Returns MIME type for supported extensions, else NULL.
     */
    const char* lookup(const char* path)
    {

        char* thing = strrchr(path,'.');
        if(strcasecmp(thing,".css") == 0)
        {


            return "text/css";
        }
        else if(strcasecmp(thing,".html") == 0)
        {


            return "text/html"; 
        }
        else if(strcasecmp(thing,".gif") == 0)
        {


            return "image/gif";
        }
        else if(strcasecmp(thing,".ico") == 0)
        {


            return "image/x-icon";
        }
        else if(strcasecmp(thing,".jpg") == 0)
        {


            return "image/jpeg";
        }
        else if(strcasecmp(thing,".js") == 0)
        {


            return "text/javascript";
        }
        else if(strcasecmp(thing,".php") == 0)
        {


            return "text/x-php";
        }
        else if(strcasecmp(thing,".png") == 0)
        {


            return "image/png";
        }
        else
        {


            return NULL;
        }

    }

    /**
     * Parses a request-line, storing its absolute-path at abs_path 
     * and its query string at query, both of which are assumed
     * to be at least of length LimitRequestLine + 1.
     */
    bool parse(const char* line, char* abs_path, char* query)
    {
        char targetline[LimitRequestLine + 1];
        char queryline[LimitRequestLine + 1];

        const char* haystack = line;
        const char* needle = strstr(haystack, " ");
        if (needle == NULL)
        {
            error(400);
            return false;
        }
        char method[needle - haystack + 1];
        strncpy(method, haystack, needle - haystack );
        method[needle - haystack ] = '\0';


        haystack = needle + 1;
        const char* request = needle + 1;
        needle = strstr(haystack, " ");
        if (needle == NULL)
        {
            error(400);
            return false;
        }
        char request_arr[needle - haystack + 1];
        strncpy(request_arr, haystack, needle - haystack );
        request_arr[needle - haystack ] = '\0';

        const char* needle2 = strstr(request, "?");
        if (needle2 == NULL)
        {
            int request_size = strlen(request_arr);
            char target[request_size + 1];
            strcpy(target,request_arr);

            strcpy(targetline,target);
            targetline[request_size] = '\0';

            if(targetline[0] != '/')
            {

                error(501);
                return false;
            }
            request = NULL;

        }
        else
        {
            char target[needle2 - request + 1];
            strncpy(target,request,needle2 - request);
            target[needle2 - request] = '\0';
            strcpy(targetline,target);

            request = needle2 + 1;
            needle2 = strstr(request," ");
            char query2[needle2 - request + 1];
            strncpy(query2,request,needle2 - request);
            query2[needle2 - request] = '\0';
            strcpy(queryline,query2);
            if(targetline[0] != '/')
            {

                error(501);
                return false;
            }


        }


        haystack = needle + 1;
        needle = strstr(haystack, "\r\n");
        if (needle == NULL)
        {
            error(400);
            return false;
        }
        char http_arr[needle - haystack + 1];
        strncpy(http_arr, haystack, needle - haystack );
        http_arr[needle - haystack ] = '\0';



        int j = 0;
        int k = strlen(line);
        while(j < k)
        {
            if((int)line[j] == 34)
            {
                error(400);
                return false;
            }
            j++;
        }

        if(strcmp("GET", method) != 0)
        {
            error(405);

            return false;
        }

        if(strcmp("HTTP/1.1", http_arr) != 0)
        {
            error(505);

            return false;
        }

            strcpy(abs_path,targetline);
            if(request != NULL)
            {
                strcpy(query,queryline);
            }

            return true;






    }
8
  • "one of these things is not like the others". Compare this char declaration char target[request_size]; to the other 5 char declarations. If this helps you solve the problem, be sure to add an answer, then wait the requisite 2 days to accept it, so your solution can help others. If it doesn't help, gdb can! May 13 '16 at 13:39
  • @DinoCoderSaurus thanks for responding to my question but i am not getting what you are saying can you please explain me the problem. Please help me out stuck here for two weeks
    – Karan Garg
    May 14 '16 at 10:17
  • All of your char declarations have a + 1 to allow space for the null terminator. char target[request_size]; does not allocate enough space for the null terminator. That may not be the only thing wrong with your code. May 14 '16 at 11:46
  • Thanks @DinoCoderSaurus for helping me now i am getting all greens with check50 but when i run it myself i get error 500 internal server error Please tell me why is that happening. And i have posted my new code with load function.Please help. Also if you are on slack please help me there as i can tell you my problem there more fastly and results will be better. And thanks again
    – Karan Garg
    May 14 '16 at 13:39
  • You're welcome. I do not have slack. This should really be a new question because the original question is answered. To start with FILE* open = fopen("*file","r"); 1) file is already open in main (look at the transfer function. and 2) you are telling program to open file named "*file". I will take some more time to review your load function and will post back. May 14 '16 at 14:10

You must log in to answer this question.

Browse other questions tagged .