cse15l-lab-reports

Lab Report 2 - Servers and Bugs

Yangyang Liu
CSE 15L Section B02
PID: A17360266

Part 1 - StringServer

Here is an implementation for a web server called StringServer.
It keeps track of a single string that get added to by incoming requests.

Note: It makes use of the Server.java file found here.

// In StringServer.java file

import java.io.IOException;
import java.net.URI;

class StringHandler implements URLHandler {
    
    String str = "";

    public String handleRequest(URI url) {
        if (url.getPath().equals("/")) {
            if (str.equals("")) {
                return "Start adding words";
            }
            return str;
        }
        else if (url.getPath().contains("/add-message")) {
            String[] parameters = url.getQuery().split("=");
            if (parameters[0].equals("s")) {
                str += (parameters[1] + "\n");
                return str;
            }
        }
        return "404 Not Found!";
    }
}

class StringServer {
    public static void main(String[] args) throws IOException {
        if(args.length == 0){
            System.out.println("Missing port number! Try any number between 1024 to 49151");
            return;
        }

        int port = Integer.parseInt(args[0]);

        Server.start(port, new StringHandler());
    }
}

Screenshot #1

/add-message?s=firstWord

Which methods in your code are called? What are the relevant arguments to those methods, and the values of any relevant fields of the class?

Note: Some method headers were found from Java documentation

  1. handleRequest(URL url) method of the StringHandler class
    • Argument(s): url with value http://localhost:5454/add-message?s=firstWord
    • Relevant Fields: str with initial value ""

  2. getPath() method of the URI class
    • Called on https://localhost:5454/add-message?s=firstWord in handleRequest method
    • No relevant arguments
    • Returns the path component of the URI: /add-message

  3. equals(Object obj)
    • Called in the handleRequest method to compare path component of the URI with the string "/"
    • Argument(s): obj with value "\"
    • Returns boolean value false

  4. contains(CharSequence s) method
    • Called in the handleRequest method to check if path component of URI contains the string "/add-message"
    • Arguments(s): s with value "/add-message"
    • Returns boolean value true

  5. getQuery() method of the URI class
    • Called on https://localhost:5454/add-message?s=firstWord in handleRequest method
    • No relevant arguments
    • Returns the query component of the URI: "s=firstWord"

  6. split(String regex) method
    • Called on "s=firstWord" in handleRequest method
    • Argument(s): regex with value "="
    • Returns array of strings: {"s", "firstWord"}

How do the values of any relevant fields of the class change from this specific request? If no values got changed, explain why.

Screenshot #2

/add-message?s=secondWord

Which methods in your code are called? What are the relevant arguments to those methods, and the values of any relevant fields of the class?

The called methods are the same as in Screenshot #1.

  1. handleRequest(URL url) method of the StringHandler class
    • Argument(s): url with value http://localhost:5454/add-message?s=secondWord
    • Relevant Fields: str with initial value "firstWord\n"

  2. getPath() method of the URI class
    • Called on https://localhost:5454/add-message?s=secondWord in handleRequest method
    • No relevant arguments
    • Returns the path component of the URI: /add-message

  3. equals(Object obj)
    • Called in the handleRequest method to compare path component of the URI with the string "/"
    • Argument(s): obj with value "\"
    • Returns boolean value false

  4. contains(CharSequence s) method
    • Called in the handleRequest method to check if path component of URI contains the string "/add-message"
    • Arguments(s): s with value "/add-message"
    • Returns boolean value true

  5. getQuery() method of the URI class
    • Called on https://localhost:5454/add-message?s=secondWord in handleRequest method
    • No relevant arguments
    • Returns the query component of the URI: "s=secondWord"

  6. split(String regex) method
    • Called on "s=secondWord" in handleRequest method
    • Argument(s): regex with value "="
    • Returns array of strings: {"s", "secondWord"}

How do the values of any relevant fields of the class change from this specific request? If no values got changed, explain why.

Part 2 - Bugs

reverseInPlace in ArrayExamples.java

  1. Failure-Inducing Input
@Test
public void testReverseInPlace() {
    int[] input = {1, 2, 3, 4, 5};
    ArrayExamples.reverseInPlace(input);
    assertArrayEquals(new int[]{5, 4, 3, 2, 1}, input);
}


  1. Nonfailure-Inducing Input
@Test public void testReverseInPlace() {
    int[] input = {0};
    ArrayExamples.reverseInPlace(input);
    assertArrayEquals(new int[]{0}, input);
}


  1. Symptoms
    • Passed Test:

            

    • Failed Test:

            

  2. Bug

    BEFORE code change:

     static void reverseInPlace(int[] arr) {
          for(int i = 0; i < arr.length; i += 1) {  // bug in for-loop
            arr[i] = arr[arr.length - i - 1]; // bug in method body
          }
     }
    

    There is a bug in this implementation for the reverseInPlace method. The method iterates through every element in the int array, replacing it with its compliment. However, this implementation causes every element to be switched twice, return an array with the same items in the same order as the original array.

    AFTER code change:

     static void reverseInPlace(int[] arr) {
          for(int i = 0; i < arr.length/2; i += 1) {  // change to arr.length
            int temp = arr[i];  // added line
            arr[i] = arr[arr.length - i - 1];
            arr[arr.length - i - 1] = temp;  // added line
          }
     }
    

    Here, the method iterates only through the first half of the array and involves a new temp variable that stores the value of the original indexed value. As a result, every element is switched once and a reversed array is returned.

Part 3 - Reflection

I learned a lot about servers during the Week 2 Lab. I didn’t know that a (simple) server was so easy to start. It only requires two short Java files for you start your own server. From that lab, I also learned how the “?” query could be used in websites. All the things we learned about servers and URLs relates back to the first couple weeks where we learned about paths.

☺︎