cse15l-lab-reports

Lab Report 2 - Servers and Bugs (Week 3)

Part 1

Here’s my implementation of the StringServer web server that keeps track of a single string that is added to by requests.

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

class StringHandler implements URLHandler {
    String webString = "";
    public String handleRequest(URI url) {
        if (url.getPath().equals("/add-message")) {
            String[] params = url.getQuery().split("=");
            if (params[0].equals("s")) {
                webString += params[1] + "\n";
                return webString;
            }
        } 
        return "404 Not Found!";
    }
}

class StringServer {
    public static void main(String[] args) throws IOException {
        Server.start(4000, new StringHandler());
    }
}

Here are two examples of using /add-message

For the first screenshot:

For the second screenshot, the same logic applies as for the first screenshot:

Part 2

As a reminder if you don’t have the code, the buggy reversed method of the ArrayExamples class was as follows:

static int[] reversed(int[] arr) {
  int[] newArray = new int[arr.length];
  for(int i = 0; i < arr.length; i += 1) {
    arr[i] = newArray[arr.length - i - 1];
  }
  return arr;
}
@Test
public void testReversedWithNonzeroValues() {
  int[] input = {1, 2};
  assertArrayEquals(new int[]{2, 1}, ArrayExamples.reversed(input));
}
@Test
public void testReversedWithDefaultValues() {
    int[] input = {0, 0};
    assertArrayEquals(new int[]{0, 0}, ArrayExamples.reversed(input));
}

And here’s the symptom (the output of running the JUnit tests):

I also went ahead and fixed the reversed method, and here’s the before-and-after of the change.

Before:

static int[] reversed(int[] arr) {
  int[] newArray = new int[arr.length];
  for(int i = 0; i < arr.length; i += 1) {
    arr[i] = newArray[arr.length - i - 1];
  }
  return arr;
}

After:

static int[] reversed(int[] arr) {
  int[] newArray = new int[arr.length];
  for(int i = 0; i < arr.length; i += 1) {
    newArray[i] = arr[arr.length - i - 1];
  }
  return newArray;
}

To see why this fix actually addresses the issue, here’s a line of reasoning that you can take:

And the bug is fixed! This can easily be verified by recompiling and running JUnit again:

Part 3

I learned a lot from the week 2 lab, since I previously didn’t really understand how web servers worked. Through the process, I first learned about the fact that these web servers take up ports on a computer, and thus two different servers on the same computer require different ports. The ports are part of the domain, but we usually don’t see them in URLs because there are default ports that are used, and the browser doesn’t need to show that extraneous information usually. I also learned that servers don’t have to be running on my local computer all the time; I can leave that task up to the remote computers! Doing that also allows me to easily send requests to the web server from other computers. Finally, I learned about the fact that a web server has to have a backend implementation that handles URL requests and returns output as web pages to the user. Then by writing the “SearchEngine” class, I got hands-on experience on how to process the URL in order to keep track of a list of strings, which was a pretty simple case and it wasn’t necessarily a styled web page, but it ultimately helped me comprehend the web server process of handling URL requests.