0

My server seems to be working fine, except when I submit form in hello.html and load hello.php. It results in Error 500 (Internal Server Error).

Also, when I use check50 it results in 'Unexpected end of input'.

Please help. I have been trying to fix it for 3 days now.

load() function -

bool load(FILE* file, BYTE** content, size_t* length)
{
    BYTE* data = malloc(128);
    unsigned int cap = 128;
    unsigned int l = 0;
    BYTE c;

    if (file == NULL)
        {
            return false;
        }

    while(1)
        {
            if (l >= (cap-1))
                {
                    cap = (cap*2);
                    data = realloc(data,cap);
                }

            c = fgetc(file);

            if (feof(file))
                {
                    break;
                }

            else
                {
                    fseek(file,-1,SEEK_CUR);
                    c = fgetc(file);
                    *(data+l) = c;
                    l++;
                }
        }

    *length = (size_t)l;
    *content = data;
    return true;
}

lookup() function -

const char* lookup(const char* path)
{
    int l = strlen(path);
    char* path1 = malloc(l + 1);

    int i;

    for(i=0; i<=l; i++)
        {
            if ((*(path+i) >= 'A') && (*(path+i) <= 'Z'))
                {
                    *(path1+i) = (*(path+i) + 32);
                }

            else
                {
                    *(path1+i) = *(path+i);
                }
        }

    for(i=l;i>0;i--)
        {
            if (*(path1+i) == '.')
                {
                    i++;
                    break;
                }
        }

    char* ext = malloc(6);
    int j=0;

    for(;i<=l;i++)
        {
            if (*(path1+i) == '\0')
                {
                    *(ext+j) = '\0';
                    break;
                }

            else
                {
                    *(ext+j) = *(path1+i);
                    j++;
                }
        }

    if (!strcmp(ext,"css"))
        {
            return "text/css";
        }

    else if (!strcmp(ext,"html"))
        {
            return "text/html";
        }

    else if (!strcmp(ext,"gif"))
        {
            return "image/gif";
        }

    else if (!strcmp(ext,"ico"))
        {
            return "image/x-icon";
        }

    else if (!strcmp(ext,"jpg"))
        {
            return "image/jpeg";
        }

    else if (!strcmp(ext,"js"))
        {
            return "text/javascript";
        }

    else if (!strcmp(ext,"php"))
        {
            return "text/x-php";
        }

    else if (!strcmp(ext,"png"))
        {
            return "image/png";
        }

    return NULL;  

}

parse() function

bool parse(const char* line, char* abs_path, char* query)
{
    char* method = malloc(4);
    int i,j;
    int l = strlen(line);

    for (i=0;i<l;i++)
        {
            if (*(line+i) == ' ')
                {
                    *(method+i) = '\0';
                    i++;
                    break;
                }

            else
                {
                    *(method+i) = *(line+i);
                }
        }

    if(strcmp(method,"GET"))
        {
            error(405);
            return false;
        }

    j=0;
    char* target = malloc(LimitRequestLine + 100);
    for(;i<l;i++)
        {
            if (*(line+i) == ' ')
                {
                    *(target+j) = '\0';
                    i++;
                    break;
                }

            else
                {
                    *(target+j) = *(line+i);
                    j++;
                }
        }

    if(*(target) != '/')
        {
            error(501);
            return false;
        }

    for(j=0;j<strlen(target);j++)
        {
            if (*(target+j) == '"')
                {
                    error(400);
                    return false;
                }
        }

    j=0;
    char* version = malloc(9);
    for(;;i++)
        {
            if ((*(line+i) == '\n') || (*(line+i) == '\r'))
                {
                    *(version+j) = '\0';
                    break;
                }

            else
                {
                    *(version+j) = *(line+i);
                    j++;
                }
        }

    if(strcmp(version,"HTTP/1.1"))
        {
            error(505);
            return false;
        }


    i=0; j=0;
    char* pth = malloc(LimitRequestLine + 100);

    while(1)
        {
            if ((*(target+i) == '?') || (*(target+i) == '\0'))
                {
                    *(pth+j) = '\0';
                    break;
                }

            else
                {
                    *(pth+j) = *(target+i);
                    j++;
                    i++;
                }
        }

    char* q = malloc(LimitRequestLine +100);

    if (*(target+i) == '?')
        {
            j=0;
            i++;

            while(1)
                {
                    if (*(target+i) == '\0')
                        {
                            *(q+j) = '\0';
                            strcpy(abs_path,pth);
                            strcpy(query,q);
                            return true;
                            break;
                        }

                    else
                        {
                            *(q+j) = *(target+i);
                            i++;
                            j++;
                        }
                }
        }

    else
        {
            *q = '\0';
            strcpy(query,q);
            strcpy(abs_path,pth);
            return true;
        }
}

indexes() function -

char* indexes(const char* path)
{
    int l = strlen(path);
    char* temp = malloc(l+12);
    *temp = '\0';
    strcat(temp,path);
    strcat(temp,"/index.php");

    FILE* f1 = fopen(temp,"r");

    if (f1 == NULL)
        {
            char* temp2 = malloc(l+12);
            *temp2 = '\0';
            strcat(temp2,path);
            strcat(temp2,"/index.html");

            FILE* f2 = fopen(temp2,"r");

            if (f2 == NULL)
                {
                    return NULL;
                }

            else
                {
                    return temp2;
                }
        }

    else
        {
            return temp;
        }
}

1 Answer 1

0

check50 "end of input" problem is the check50 server. It has not processed your submission. The only advice is "keep trying".

The 500 error is probably from the load function: you cannot use fseek on a pipe. More info here.

3
  • I suppose I need to open the file using popen(), and not use fseek(). But can you please suggest how to detect the file type, so that I use fopen() and popen appropriately?
    – abhishek
    Jun 28, 2016 at 12:41
  • That is not what you have to do. You can use fgetc (or fread). The trick is in handling the eof properly. In the load function, you do not care what type of file, you just want to grab the contents. Jun 28, 2016 at 12:48
  • Looks like, fopen() can also be used for opening a process. And, now it's working.
    – abhishek
    Jun 28, 2016 at 12:50

You must log in to answer this question.

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