WebFund 2024F: Assignment 1

From Soma-notes
Revision as of 02:51, 27 September 2024 by Soma (talk | contribs)

This assignment is not yet finalized.

Please submit the answers to the following questions via Brightspace by October 4, 2024. There are ?? points in ?? questions.

Submit your answers as a plain text file following this template. Name your answer file "comp2406-assign1-<username>.txt" (where username is your MyCarletonOne username). Please make sure to use your correct student ID number otherwise your grades may not be properly recorded.

Your answers will be parsed by a script in order to help with grading so please preserve the format of the template. No other formats will be accepted.

Note that the file should be a text file, preferably a UNIX text file (so LF line endings, not CRLF). (See the Wikipedia page on text files to learn more.)

You may use this validator page to make sure your answer file is properly named and formatted.

Don't forget to include what outside resources you used to complete each of your answers, including other students, man pages, and web resources (including AI services). You do not need to list help from the instructor, TA, or information covered in lecture or tutorial.

Please solve each question rather than look up their answers. Solutions where the answer was found through an online search or answered by a friend or AI will likely get partial or no marks. The questions can only be fully answered by documenting your journey to find the answer rather than the answer itself. Outside resources and other individuals should be used to help with specific technical problems (e.g., JavaScript syntax and semantics), not to solve the problem itself. Your solution should be your own.

Questions

  1. [4] How could you change simpleserver2 so that it loaded the page not found template from a file (rather than it being included in the source)? The template should be loaded when the server starts. Note that your solution should only use functions or methods that are in the tutorial code.
    1. [2] What code needs to be added, changed, or removed? Be precise.
    2. [1] Why do you think your solution is correct?
    3. [1] What mistakes, errors, or other problems arose while you were developing your solution?
  2. [5] How could you change line 80 of simpleserver2.js (the assignment of origpath) so that it directly extracted the path (pathname) from the request URL string, instead of first converting it into a URL object? You can use any built-in JavaScript string manipluation facilities, including regular expressions. Make sure your solution does not include the protocol, hostname, port number, query, or fragment.
    1. [2] What is your solution? Please give the precise code.
    2. [1] What resources did you use while developing your solution?
    3. [1] How did you test your solution to make sure it was correct?
    4. [1] What mistakes, errors, or other problems arose while you were developing your solution?

Code

Download simpleserver2.js

// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (C) 2024 Anil Somayaji
//
// simpleserver2.js
// for COMP 2406 (Fall 2024), Carleton University
// 
// Initial version: Sept 26, 2024
//
// run with the following command:
//    deno run --allow-net --allow-read simpleserver2.js
//

const status_NOT_FOUND = 404;
const status_OK = 200;

function MIMEtype(filename) {

    const MIME_TYPES = {
        'css': 'text/css',
        'gif': 'image/gif',
        'htm': 'text/html',
        'html': 'text/html',
        'ico': 'image/x-icon',
        'jpeg': 'image/jpeg',
        'jpg': 'image/jpeg',
        'js': 'text/javascript',
        'json': 'application/json',
        'png': 'image/png',
        'txt': 'text/text'
    };

    var extension = "";
    
    if (filename) {
        extension = filename.slice(filename.lastIndexOf('.')+1).toLowerCase();
    }

    return MIME_TYPES[extension] || "application/octet-stream";
};


function template_header(title) {
    return `<!DOCTYPE html>
<html>
  <head>
    <title>${title}</title>
  </head>
`
}

function template_notFound(path) {
    return template_header("Page not found") +
        `<body>
<h1>Page not found</h1>

<p>Sorry, the requested page was not found.</p>
</body>
</html>
`
}

async function fileData(path) {
    var contents, status, contentType;
    
    try {
        contents = await Deno.readFile("./www" + path);
        status = status_OK;
        contentType = MIMEtype(path);
    } catch (e) {
        contents = template_notFound(path);
        status = status_NOT_FOUND;
        contentType = "text/html";
    }
    
    return { contents, status, contentType };
}

async function handler(req) {

    var origpath = new URL(req.url).pathname;    
    var path = origpath;
    
    if (path === "/") {
        path = "/index.html";
    }
    
    var r = await fileData(path);

    console.log(`${r.status} ${req.method} ${r.contentType} ${origpath}`); 

    return new Response(r.contents,
                        {status: r.status,
                         headers: {
                             "content-type": r.contentType,
                         }});
}

Deno.serve(handler);