0

When I load a page from my webserver all I get is a blank page if it's HTTP or a tiny white box with a black outline instead of the contents of the pages. What functions should I look at to solve this? I also get error 500 when requesting PHP files and error 501 when requesting favicon.ico.

Lookup:

const char* lookup(const char* path)
{
    bool tFound = false;
    char* fType = malloc(sizeof(char));
    int fIncriment = 0;
    if(path == NULL)
    {
        return NULL;
    }
    for(int i = 0; i<strlen(path);i++)
    {
        if (path[i] == '.')
        {
            tFound = true;
        }
        if(tFound == true)
        {
            fType[fIncriment] = path[i];
            fIncriment++;
        }
    }
    if(strcasecmp(fType,".css") == 0)
    {
        free(fType);
        return "text/css";
    }
    else if(strcasecmp(fType,".html") == 0)
    {
        free(fType);
        return "text/html";
    }
    else if(strcasecmp(fType,".gif") == 0)
    {
        free(fType);
        return "text/css";
    }
    else if(strcasecmp(fType,".css") == 0)
    {
        free(fType);
        return "x-icon/ico";
    }
    else if(strcasecmp(fType,".jpg") == 0)
    {
        free(fType);
        return "image/jpeg";
    }
    else if(strcasecmp(fType,".js") == 0)
    {
        free(fType);
        return "text/javascript";
    }
    else if(strcasecmp(fType,".php") == 0)
    {
        free(fType);
        return "text/x-php";
    }
    else if(strcasecmp(fType,".png") == 0)
    {
        free(fType);
        return "image/png";
    }
    free(fType);
    return NULL;
}

Parse:

bool parse(const char* line, char* abs_path, char* query)
{
    char* lnCopy = strdup(line); // creates a copy of a non constant version of the line 
    char* method = strtok(lnCopy," "); // gets the first part of the line
    char* request_target =  strtok(NULL, " "); // second part
    char* version = strtok(NULL,"\r"); // third part
    if(strcasecmp(version,"HTTP/1.1") != 0) // checks to make sure its the right HTTP version
        {
            error(505);
            return false;
        }
        if(strcmp(method,"GET") != 0)
        {
            error(405);
            return false;
        }
        for(int i = 0; i< strlen(request_target); i++)
        {
            if(request_target[i] == 34)
            {
                error(400);
                return false;
            } 
        }
        if(request_target[0] != '/')
        {
            error(501);
            return false;
        }

        bool qCheck = false;
    for(int i =0; i< strlen(request_target);i++)
    {
        if(qCheck == false)
        {
            abs_path[i]=request_target[i];
            if(strcmp(&request_target[i],"?") == 0)
            {
                abs_path[i] = '\0';
                qCheck = true;
            }
        }
        else if(qCheck == true)
        {
            if(strcmp(&request_target[i],"?") != 0)
            {
                query[i] = request_target[i];
            }
        }
    }
    if(qCheck == false)
    {
        abs_path[strlen(request_target)] = '\0';
        query[0] = '\0';
    }
    else if(qCheck == true)
    {
        abs_path[strlen(request_target)-strlen(query)] = '\0';
        query[strlen(query)] = '\0';
    }
    return true;
}

Load:

bool load(FILE* file, BYTE** content, size_t* length)
{
    if(file == NULL)
    {
        error(404);
        return false;
    }
    BYTE *storage = malloc(sizeof(BYTES));
    fread(storage,sizeof(storage),1,file);
    *length = BYTES;
    *content = &storage[0];
    return true;
}

Indexes:

char* indexes(const char* path)
{
    FILE *filetest;
    FILE *filetestTheSequal;
    char* index = malloc(sizeof(char*));
    if (path == NULL)
    {
        return NULL;
    }
    filetest = fopen("index.php","r");
    filetestTheSequal = fopen("index.html","r");
    if(filetest == NULL && filetestTheSequal ==NULL)
    {
        return NULL;
    }
    else if(filetest != NULL && filetestTheSequal == NULL)
    {
        fclose(filetest);
        fclose(filetestTheSequal);
        return  index = "/path/to/a/directory/index.php";
    }
    else if(filetestTheSequal != NULL && filetest == NULL)
    {
        fclose(filetest);
        fclose(filetestTheSequal);
        return index = "/path/to/a/directory/index.html";
    }
    free(index);
    fclose(filetest);
    fclose(filetestTheSequal);
    return NULL;
}

Full Code: http://pastebin.com/3e319PEB

1 Answer 1

1

At a quick glance, there's two glaring problems:

lookup

A little .css/.ico confusion.

load

It only reads at most 8 bytes from any file. fread(storage,sizeof(storage),1,file);. storage is a pointer, a pointer is 8 bytes. But wait there's more! If you change that to X, it will only read X bytes of the file. You need some kind of loop, and you will need to read up on realloc too.

4
  • Thanks for pointing out where I was going wrong. I ended up having to use some fseek logic from pset4 to make load work. looping just caused segfaults
    – Kypore
    Commented Aug 30, 2016 at 16:17
  • Beware of fseek. php requests are pipes, not files, and fseek doesn't work on pipes. Commented Aug 30, 2016 at 17:07
  • How should I go about doing PHP then? I've been having issues with opening PHP and haven't been able to figure out why. That's probably why.
    – Kypore
    Commented Aug 30, 2016 at 17:29
  • I think you need to make your looping work. I have no other advice or suggestions to offer. -oo- Commented Aug 31, 2016 at 13:18

You must log in to answer this question.

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