WebFund 2024F: Assignment 1: Difference between revisions

From Soma-notes
No edit summary
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
'''This assignment is not yet finalized.'''
Please submit the answers to the following questions via Brightspace by October 7, 2024. There are 20 points in 6 questions.
 
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 [https://homeostasis.scs.carleton.ca/~soma/webfund-2024f/templates/comp2406-a1-template.txt 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.
Submit your answers as a plain text file following [https://homeostasis.scs.carleton.ca/~soma/webfund-2024f/templates/comp2406-a1-template.txt 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.
Line 11: Line 9:
<b>You may use [https://homeostasis.scs.carleton.ca/~soma/webfund-2024f/validators/a1-validator.html this validator page] to make sure your answer file is properly named and formatted.</b>
<b>You may use [https://homeostasis.scs.carleton.ca/~soma/webfund-2024f/validators/a1-validator.html this validator page] to make sure your answer file is properly named and formatted.</b>


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, and the journey by which you reach it should be your own.


'''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, and the journey by which you reach it should be your own.
Having said this, for each question, please 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.


==Questions==
==Questions==
Line 20: Line 18:
<li>[2] What directory does simpleserver2 use for serving content? How did you verify your answer was correct?
<li>[2] What directory does simpleserver2 use for serving content? How did you verify your answer was correct?
<li>[2] Does simpleserver2 support serving PDFs? (What does "support" in this context mean?) How did you verify that your answer was correct?
<li>[2] Does simpleserver2 support serving PDFs? (What does "support" in this context mean?) How did you verify that your answer was correct?
<li>[3] How does the behavior of simpleserver2 change if you make MIMEtype() an async function? (Be brief and specific.) How did you check this? And what could you change in simpleserver2 to make it work as before (without removing the async function declaration)?
<li>[4] Currently simpleserver2 will serve content no matter the method of the incoming request.
<li>[4] Currently simpleserver2 will serve content no matter the method of the incoming request.
   <ol style="list-style-type:lower-alpha">
   <ol style="list-style-type:lower-alpha">

Latest revision as of 02:11, 28 September 2024

Please submit the answers to the following questions via Brightspace by October 7, 2024. There are 20 points in 6 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.

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, and the journey by which you reach it should be your own.

Having said this, for each question, please 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.

Questions

  1. [2] What directory does simpleserver2 use for serving content? How did you verify your answer was correct?
  2. [2] Does simpleserver2 support serving PDFs? (What does "support" in this context mean?) How did you verify that your answer was correct?
  3. [3] How does the behavior of simpleserver2 change if you make MIMEtype() an async function? (Be brief and specific.) How did you check this? And what could you change in simpleserver2 to make it work as before (without removing the async function declaration)?
  4. [4] Currently simpleserver2 will serve content no matter the method of the incoming request.
    1. [2] How could you modify simpleserver2 so that it only returns content on GET requests? When it receives a non-GET method, it should return the string "Method not implemented." and the status code 501.
    2. [2] What issues arose when implementing the answer to the previous question? How did you verify your answer was correct? (Hint: you'll probably want to use a command-line tool.)
  5. [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?
  6. [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);