Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions DockerFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# base image from gradescope
FROM gradescope/auto-builds:ubuntu-18.04
# make necessary directories
RUN apt-get update &&\
apt-get -y install gcc flex bison build-essential siege apache2-utils libssl-dev &&\
# change ApacheBench request HTTP version to 1.1
perl -pi -e 's/HTTP\/1.0/HTTP\/1.1/g' /usr/bin/ab

WORKDIR /home
46 changes: 46 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
SRC_DIR := src
OBJ_DIR := obj
# all src files
SRC := $(wildcard $(SRC_DIR)/*.c)
# all objects
OBJ := $(OBJ_DIR)/y.tab.o $(OBJ_DIR)/lex.yy.o $(OBJ_DIR)/parse.o $(OBJ_DIR)/example.o
#OBJ_SERVER := $(OBJ_DIR)/y.tab.o $(OBJ_DIR)/lex.yy.o $(OBJ_DIR)/parse.o $(OBJ_DIR)/echo_server.o
# all binaries
BIN := example echo_server echo_client
# C compiler
CC := gcc
# C PreProcessor Flag
CPPFLAGS := -Iinclude
# compiler flags
CFLAGS := -g -Wall
# DEPS = parse.h y.tab.h

default: all
all : example echo_server echo_client

example: $(OBJ)
$(CC) $^ -o $@

$(SRC_DIR)/lex.yy.c: $(SRC_DIR)/lexer.l
flex -o $@ $^

$(SRC_DIR)/y.tab.c: $(SRC_DIR)/parser.y
yacc -d $^
mv y.tab.c $@
mv y.tab.h $(SRC_DIR)/y.tab.h

$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c $(OBJ_DIR)
$(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@

echo_server: $(OBJ_DIR)/echo_server.o $(OBJ_DIR)/lex.yy.o $(OBJ_DIR)/y.tab.o $(OBJ_DIR)/parse.o
$(CC) -g -Werror $^ -o $@

echo_client: $(OBJ_DIR)/echo_client.o
$(CC) -Werror $^ -o $@

$(OBJ_DIR):
mkdir $@

clean:
$(RM) $(OBJ) $(BIN) $(SRC_DIR)/lex.yy.c $(SRC_DIR)/y.tab.*
$(RM) -r $(OBJ_DIR)
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,27 @@
# ComputerNetwork_socket_labb
# project-1

This repository contains the starter code for ***CMU 15-441/641 Networking and the Internet Project 1: A Web Server Called Liso***.

## 1. Files
- `DockerFile`: Script to build the docker image for the project's environment.
- `Makefile`: Contains rules for `make`.
- `README.md`: Current document.
- `cp1`: CP1 scripts and examples.
- `cp2`: CP2 scripts and examples.
- `cp3`: CP3 scripts and examples.
- `src/`: Source code for the project.
- `src/echo_client.c`: Simple echo network client.
- `src/echo_server.c`: Simple echo network server
- `src/example.c`: Example driver for parsing.
- `src/lexer.l`: Lex/Yacc related logic.
- `src/parser.y`
- `src/parse.c`
- `include/parse.h`

## 2. Environment Setup
1. Install docker: https://www.docker.com
2. Open a terminal and navigate to the directory containing this `README.md` file.
3. Build the docker image: `docker build -t 15-441/641-project-1:latest -f ./DockerFile .`
4. Run the docker container: ``docker run -it -v `pwd`:/home/project-1/ --name <name for your container> 15-441/641-project-1 /bin/bash``
5. The starter code for the project is available at `/home/project-1/` in the container and `.` on your local machine. To make development easier, a mapping is established between these two folders. Modiying the code in one location will also effect the other one. This means that you can use an IDE to write code on your local machine and then seamlessly test it in the container.
6. To test your server using a web browser, you need to configure port mapping for the docker container. Simply add the argument `-p 8888:15441` to the `docker run` command to establish a mapping from `127.0.0.1:15441` in the container to `127.0.0.1:8888` on your local machine. Then you can test your server by using a web browser (e.g., Chrome) on your local machine to navigate to the URL `127.0.0.1:8888`.
2 changes: 2 additions & 0 deletions cgi/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CGI Example Code - C server-side example, Python client examples; note: it doesn't show sending of content via stdin etc.
Daemonizing C Code - helper daemonizing code
Binary file added cgi/cgi.tar.gz
Binary file not shown.
82 changes: 82 additions & 0 deletions cgi/daemonize.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/******************************************************************************
* Reference: http://www.enderunix.org/docs/eng/daemon.php *
* Modified by: Wolf Richter <wolf@cs.cmu.edu> *
* O_EXCL Bug Fix by: Ming Han <mteh@andrew.cmu.edu *
******************************************************************************/


/* daemonize includes */
#include <fcntl.h>
#include <signal.h>
#include <unistd.h>
#include <syslog.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

/***** Utility Functions *****/

/**
* internal signal handler
*/
void signal_handler(int sig)
{
switch(sig)
{
case SIGHUP:
/* rehash the server */
break;
case SIGTERM:
/* finalize and shutdown the server */
// TODO: liso_shutdown(NULL, EXIT_SUCCESS);
break;
default:
break;
/* unhandled signal */
}
}

/**
* internal function daemonizing the process
*/
int daemonize(char* lock_file)
{
/* drop to having init() as parent */
int i, lfp, pid = fork();
char str[256] = {0};
if (pid < 0) exit(EXIT_FAILURE);
if (pid > 0) exit(EXIT_SUCCESS);

setsid();

for (i = getdtablesize(); i>=0; i--)
close(i);

i = open("/dev/null", O_RDWR);
dup(i); /* stdout */
dup(i); /* stderr */
umask(027);

lfp = open(lock_file, O_RDWR|O_CREAT, 0640);

if (lfp < 0)
exit(EXIT_FAILURE); /* can not open */

if (lockf(lfp, F_TLOCK, 0) < 0)
exit(EXIT_SUCCESS); /* can not lock */

/* only first instance continues */
sprintf(str, "%d\n", getpid());
write(lfp, str, strlen(str)); /* record pid to lockfile */

signal(SIGCHLD, SIG_IGN); /* child terminate signal */

signal(SIGHUP, signal_handler); /* hangup signal */
signal(SIGTERM, signal_handler); /* software termination signal from kill */

// TODO: log --> "Successfully daemonized lisod process, pid %d."

return EXIT_SUCCESS;
}
Binary file added echo_client
Binary file not shown.
Binary file added echo_server
Binary file not shown.
Binary file added example
Binary file not shown.
28 changes: 28 additions & 0 deletions include/parse.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#define SUCCESS 0

//Header field
typedef struct
{
char header_name[4096];
char header_value[4096];
} Request_header;

//HTTP Request Header
typedef struct
{
char http_version[50];
char http_method[50];
char http_uri[4096];
Request_header *headers;
int header_count;
} Request;

Request* parse(char *buffer, int size,int socketFd);

// functions decalred in parser.y
int yyparse();
void set_parsing_options(char *buf, size_t i, Request *request);
9 changes: 9 additions & 0 deletions samples/request_get
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
GET / HTTP/1.1
Host: www.cs.cmu.edu
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8


9 changes: 9 additions & 0 deletions samples/request_head
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
HEAD / HTTP/1.1
Host: www.cs.cmu.edu
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8


82 changes: 82 additions & 0 deletions samples/request_pipeline
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
HEAD / HTTP/1.1
Host: www.cs.cmu.edu
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8

DELETE /~prs/15-441-F15/ HTTP/1.1
Host: www.cs.cmu.edu
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8

GET / HTTP/1.1
Host: www.cs.cmu.edu
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8

HEAD / HTTP/1.5
Host: www.cs.cmu.edu
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8

HEAD / HTTP/1.1
Host: www.cs.cmu.edu
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8

HEAD / HTTP/1.1
Host: www.cs.cmu.edu
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8

HAHE /~prs/15-441-F15/ HTTP/1.10
Host: www.cs.cmu.edu
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8

HOHO /~prs/15-441-F15/ HTTP/1.1
Host: www.cs.cmu.edu
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8

HAHA /~prs/15-441-F15/ HTTP/1.1
Host: www.cs.cmu.edu
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8

GET
/~prs/15-441-F15/ HTTP/1.1
Host: www.cs.cmu.edu
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8


9 changes: 9 additions & 0 deletions samples/request_post
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
POST / HTTP/1.1
Host: www.cs.cmu.edu
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8


8 changes: 8 additions & 0 deletions samples/sample_400
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
GET
/~prs/15-441-F15/ HTTP/1.1
Host: www.cs.cmu.edu
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8
4 changes: 4 additions & 0 deletions samples/sample_501
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
GE /~prs/15-441-F15/ HTTP/1.1
Host: www.cs.cmu.edu


7 changes: 7 additions & 0 deletions samples/sample_error1
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
GET /~prs/15-441-F15/ HTTP/1.1
Host: www.cs.cmu.edu
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8
7 changes: 7 additions & 0 deletions samples/sample_error2
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
GET /~prs/15-441-F15/ HTTP/1.1
Host: www.cs.cmu.edu
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language en-US,en;q=0.8
7 changes: 7 additions & 0 deletions samples/sample_error3
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
GET /~prs/15-441-F15/ HTTP/1.1
: www.cs.cmu.edu
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8
7 changes: 7 additions & 0 deletions samples/sample_error4
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
GET /~prs/15-441-F15/
Host: www.cs.cmu.edu
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8
4 changes: 4 additions & 0 deletions samples/sample_request_example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
GET /~prs/15-441-F15/ HTTP/1.1
Host: www.cs.cmu.edu


9 changes: 9 additions & 0 deletions samples/sample_request_realistic
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
GET /~prs/15-441-F15/ HTTP/1.1
Host: www.cs.cmu.edu
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8


Loading