Last Updated: 2021-04-25 Sun 23:38

CSCI 4061 Lab13: Basic Sockets and HTTP Post

CODE DISTRIBUTION: lab13-code.zip

  • Download the code distribution
  • See further setup instructions below

CHANGELOG: Empty

1 Rationale

Computer Networks and programming to use them is a large and interesting area of study. This lab demonstrates the barest minimum of functionality by illustrating how to set up a Socket to communicate over a network in Unix and how to format data to request and receive data using Hyper Text Transfer Protocol (HTTP) and two of its most common methods GET and POST. The basic steps are similar to opening a FIFO and reading/writing data with it but several intermediary steps are demonstrated associated with network and encryption issues.

Grading Policy

Credit for this Lab is earned by completing the exercises here and submitting a Zip of the work to Gradescope. Students are responsible to check that the results produced locally via make test are reflected on Gradescope after submitting their completed Zip. Successful completion earns 1 Engagement Point.

Lab Exercises are open resource/open collaboration and students are encouraged to coopearte on labs. Students may submit work as groups of up to 5 to Gradescope: one person submits then adds the names of their group members to the submission.

See the full policies in the course syllabus.

2 Codepack

The codepack for this lab is linked at the top of this document. Always download it and unzip/unpack it. It should contain the following files which are briefly described.

File Use Description
QUESTIONS.txt EDIT Questions to answer: fill in the multiple choice selections in this file.
http_get.c.c Provided C file to examine / run to answer QUIZ questions
http_get_ssl.c.c Provided C file to examine / run to answer QUIZ questions
http_post_ssl.c EDIT C file to study and complete for the CODE portion. See instructions in QUESTIONS.txt
QUESTIONS.txt.bk Backup Backup copy of the original file to help revert if needed
Makefile Build Enables make test and make zip
testy Testing Test running scripts
test_lab13.org Testing Tests for this lab
test_quiz_filter Testing Used to simplify quiz checksum

3 QUESTIONS.txt File Contents

Below are the contents of the QUESTIONS.txt file for the lab. Follow the instructions in it to complete the QUIZ and CODE questions for the lab.

                           __________________

                            LAB 13 QUESTIONS
                           __________________





Lab Instructions
================

  Follow the instructions below to experiment with topics related to
  this lab.
  - For sections marked QUIZ, fill in an (X) for the appropriate
    response in this file. Use the command `make test-quiz' to see if
    all of your answers are correct.
  - For sections marked CODE, complete the code indicated. Use the
    command `make test-code' to check if your code is complete.
  - DO NOT CHANGE any parts of this file except the QUIZ sections as it
    may interfere with the tests otherwise.
  - If your `QUESTIONS.txt' file seems corrupted, restore it by copying
    over the `QUESTIONS.txt.bk' backup file.
  - When you complete the exercises, check your answers with `make test'
    and if all is well, create a zip file with `make zip' and upload it
    to Gradescope. Ensure that the Autograder there reflects your local
    results.
  - IF YOU WORK IN A GROUP only one member needs to submit and then add
    the names of their group.


QUIZ http_get.c and http_get_ssl.c
==================================

  Examine the code in `http_get.c' to get a basic sense of how sockets
  are set up for a client wishing to contact a server.

  Which of the following are the primary function calls to set up the
  socket for reading/writing?
  - ( ) socket(), connect(), activate()
  - ( ) getaddrinfo(), connect(), mksocket()
  - ( ) getaddrinfo(), socket(), connect()
  - ( ) socket(), activate(), getaddrinfo()

  After setting up the socket, which system calls are used in
  `http_get.c' to send data to the server and receive response data?
  - ( ) write() to send, read() to receive
  - ( ) senddata() to send, receiveresponse() to receive
  - ( ) put() to send, get() to receive
  - ( ) socket() to send, connect() to receive()

  The actual data that is written as the request and received from the
  server comprises an HTTP GET request/response.  What format does this
  data take?
  - ( ) It is a C struct written in binary form with fields like
    msg.host and msg.connection in it.
  - ( ) It is a complex sequence of bytes that are set up with defined
    constants like HTTP_CONNECTION and HTTP_HOST
  - ( ) It is plain text with words like "Host" and "Connection" in it
  - ( ) It is plain text which is just a web address in it like you
    would type into a browser: <http://beej.us/index.html>

  One problem that you will observe about `http_get.c' by experimenting
  with it will be that it is rejected by may sites for security
  reasons. The "Secure Socket Layer" (SSL) library can be used to make
  connections requiring encryption.  Examine the `http_get_ssl.c'
  program and identify how this encryption is used there.
  - ( ) A public/private key pair is coded which is used in a loop to
    encrypt the request and then decrypts the response. The code is very
    complex.
  - ( ) A bunch of SSL_* calls are made to create an ssl_connection
    which is used later with SSL_write() and SSL_read() identically to
    the earlier code making it very similar.
  - ( ) New options are passed to the socket() / connect() functions
    that cause it to implement encryption.  The code is almost
    identical.


CODE http_post_ssl.c
====================

  The GET method of HTTP allows one to specify a web address to get but
  all information must be passed in the address.  While highly flexible,
  the POST method allows hidden parameters to be passed to a site
  allowing the same page to respond with different data.  The program
  `http_post_ssl.c' connects to a simple page that responds to the POST
  method.

  You can get an idea of the data being submitted by visiting the
  following page:
  <https://www-users.cs.umn.edu/~kauffman/quotes_submit.html>

  On submitting data, one is taken to the page
  <https://www-users.cs.umn.edu/~kauffman/quotes.php>

  which will display data based on what was POST'ed. However, directly
  visiting <https://www-users.cs.umn.edu/~kauffman/quotes.php> will not
  display any data due to no parameters being passed via a POST.

  Complete the `http_post_ssl.c' program by lifting code from
  `http_get_ssl.c'. Once running properly, the program will produce
  output like the following (assuming that you have an active internet
  connection and the CSE Labs servers are up).

  ,----
  | >> ./http_post_ssl 1
  | Host: www-users.cs.umn.edu
  | File: /~kauffman/quotes.php
  | -------
  | REQUEST
  | -------
  | POST /~kauffman/quotes.php HTTP/1.1
  | Host: www-users.cs.umn.edu
  | Content-Type: application/x-www-form-urlencoded
  | Connection: close
  | Content-Length: 11
  | 
  | quote_num=1
  | 
  | --------
  | RESPONSE
  | --------
  | HTTP/1.1 200 OK
  | Date: Mon, 26 Apr 2021 04:14:35 GMT
  | Server: Apache
  | X-Powered-By: PHP/5.5.9-1ubuntu4.29
  | Content-Length: 62
  | Connection: close
  | Content-Type: text/html
  | 
  | I do not fear computers. I fear lack of them.
  | - Isaac Asimov
  | 
  | -------
  | 
  | >> ./http_post_ssl 28
  | Host: www-users.cs.umn.edu
  | File: /~kauffman/quotes.php
  | -------
  | REQUEST
  | -------
  | POST /~kauffman/quotes.php HTTP/1.1
  | Host: www-users.cs.umn.edu
  | Content-Type: application/x-www-form-urlencoded
  | Connection: close
  | Content-Length: 12
  | 
  | quote_num=28
  | 
  | --------
  | RESPONSE
  | --------
  | HTTP/1.1 200 OK
  | Date: Mon, 26 Apr 2021 04:14:40 GMT
  | Server: Apache
  | X-Powered-By: PHP/5.5.9-1ubuntu4.29
  | Vary: Accept-Encoding
  | Content-Length: 239
  | Connection: close
  | Content-Type: text/html
  | 
  | Pessimists, we’re told, look at a glass containing 50% air and 50% water and see it as half empty.  Optimists, in contrast, see it as half full.  Engineers, of course, understand the glass is twice as big as it needs to be.
  | - Bob Lewis
  | 
  | -------
  | 
  | >> ./http_post_ssl 98
  | Host: www-users.cs.umn.edu
  | File: /~kauffman/quotes.php
  | -------
  | REQUEST
  | -------
  | POST /~kauffman/quotes.php HTTP/1.1
  | Host: www-users.cs.umn.edu
  | Content-Type: application/x-www-form-urlencoded
  | Connection: close
  | Content-Length: 12
  | 
  | quote_num=98
  | 
  | --------
  | RESPONSE
  | --------
  | HTTP/1.1 200 OK
  | Date: Mon, 26 Apr 2021 04:14:58 GMT
  | Server: Apache
  | X-Powered-By: PHP/5.5.9-1ubuntu4.29
  | Vary: Accept-Encoding
  | Content-Length: 122
  | Connection: close
  | Content-Type: text/html
  | 
  | I don’t know what the language of the year 2000 will look like, but I know it will be called Fortran.
  | - CA Hoare, 1982
  | 
  | -------
  `----

4 Submission

Follow the instructions at the end of Lab01 if you need a refresher on how to upload your completed lab zip to Gradescope.


Author: Chris Kauffman (kauffman@umn.edu)
Date: 2021-04-25 Sun 23:38