Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ian Cameron C Web Server #306

Open
wants to merge 13 commits into
base: master
Choose a base branch
from

Conversation

codypeak
Copy link

No description provided.

Copy link

@codejoncode codejoncode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good start Ian. You're going to need a response_length. You also need to include the date. Some functions you can use are included on the rep. "you'll want to look at the time() and localtime() functions, both of which are already included in the time.h header file."

int response_length = sprintf(response, "%s\n"
        "Date: %s\n"
        "Connection: close\n"
        "Content-Length: %d\n"
        "Content-Type: %s\n"
        "\n"
        "%s\n",
        header, buffer, content_length, content_type, new_body);

Sprintf provides a
Return Value
If successful, the total number of characters written is returned excluding the null-character appended at the end of the string, otherwise a negative number is returned in case of failure.
https://www.tutorialspoint.com/c_standard_library/c_function_sprintf.htm

Copy link

@codejoncode codejoncode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Time functionality seems to work well.

A different approach :

time_t t1 = time(NULL);
    struct tm *ltime = localtime(&t1);

    int response_length = sprintf(response,
        "%s\n"
        "Date: %s" // asctime adds its own newline
        "Connection: close\n"
        "Content-Length: %d\n"
        "Content-Type: %s\n"
        "\n", // End of HTTP header

        header,
        asctime(ltime),
        content_length,
        content_type
    );

asctime automatically adds a new line for you.

Copy link

@codejoncode codejoncode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

void get_file(int fd, struct cache *cache, char *request_path)
{
    ///////////////////
    // IMPLEMENT ME! //
    ///////////////////
    (void)fd;
    (void)cache;
    //read file
    char filepath[4096];
    struct file_data *file_data; //loads file into memory and returns pointer to data
    char *mime_type;
    //fetch file
    snprintf(filepath, sizeof filepath, "%s/%s", SERVER_ROOT, request_path); 
    if (entry == NULL)
        {
    filedata = file_load(filepath);

        if (filedata == NULL) 
            {
                // TODO: make this non-fatal
                fprintf(stderr, "cannot find file\n");
                exit(3);
            }
        }
    //get mimetype
    mime_type = mime_type_get(filepath);
    //send file out
    send_response(fd, "HTTP/1.1 200 OK", mime_type, filedata->data, filedata->size);
    //then free data bc loaded these data into memory
    file_free(filedata);
    //server files v root
}

we will have to get rid of the voids statements where you cast to void (void)
filedata = file_load(filepath) should not be in a conditional but called everytime then we do a check on filedata to see if it is null.

if filedata == null call resp_404

if (filedata == NULL) {
        // TODO: make this non-fatal
        resp_404(fd);
        return; 
    }

Then in your handle http request function on the else clause replace your resp_404 with get_file(fd, NULL, path);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants