Skip to content
forked from flo-12/webserv

Our own HTTP web server in C++

Notifications You must be signed in to change notification settings

dubmix/42-Webserv

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

98 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Webserv 🏄‍♂️

This 42 project is about writing your own HyperText Transfer Protocol server in C++.

Summary

3.1 CGI

Introduction

The primary function of a web server is to store, process, and deliver web pages to clients. The communication between client and server takes place using HTTP requests, such as GET, POST or DELETE to name just a few. A user agent, commonly a web browser, initiates communication by requesting a specific resource via HTTP and the server responds with the content of that resource or an error message if unable to do so (i.e. Error 404 for when the resource doesn't exist).

Here is an example of what happens after launching our webserv and entering http://127.0.0.1:18000 in Firefox:

webserv1

In the console (F12), we can see the 4 GET requests sent by the browser to the server:

webserv2

And the response from the server (HTML, CSS, and JS):

webserv3

Usage

Warning: Makefile is configured for Linux use only.

  • Compile with make
  • Then run with ./webserv + (optional) CONFIGFILE | Example: ./webserv myserver.conf
  • Running the program with no argument will load default.conf by default
  • Access the server through a browser at address http://127.0.0.1:18000
  • To shut the server down, use Ctrl + C

Fundamentals

CGI

CGI stands for Common Gateway Interface. Thanks to this interface, a web server can execute an external program and return the result of that execution. For example, if you click on the CGI button of our web page, the address will be http://127.0.0.1:18000/index.php. You guessed it, the client wouldn't know what to make of .php files by default, and this is why we need a CGI here.

In order to execute a PHP file, we need the PHP executable usually located at /usr/bin/php on Linux. The job of the CGI is to execute the PHP file and retrieve the result of that execution.

In our website, we use PHP to execute a C++ program called RPN (Reverse Polish Notation, mathematical notation in which operators follow their operands, ex: 4 4 5 + +). After entering an operation in the submit field, a POST request containing the string is sent to the server. The content of the string is passed as argument to the RPN program, and the result of the execution gets embedded in an HTML body thanks to PHP. The resulting HTML response can then be sent to the browser, interpreted, and displayed as a page:

webserv4

This project was done in collaboration by flo-12, dubmix and Linuswidmer.

Releases

No releases published

Packages

No packages published

Languages

  • C++ 65.4%
  • C 13.7%
  • HTML 8.1%
  • Python 7.0%
  • Makefile 1.6%
  • CSS 1.5%
  • Other 2.7%