Giter Club home page Giter Club logo

webserv's Introduction

README.md

Webserv

HTTP/1.1 compatible web server in C++ using I/O multiplexing.

Motivation and Considerations

Webserv is crafted as part of the challenging curriculum at School 42, focusing on developing a HTTP server in C++ 98. Key requirements include:

  • Non-blocking socket read/write using poll()(or equivalent).
  • Ensure the server never blocks(non-blocking I/O is necessary).
  • Ensure the server never hangs indefinitely.
  • Ability to serve a fully static website.
  • Support for GET, POST, and DELETE methods.
  • Capability to listen to multiple ports. These elements combine to create a robust, efficient web server.

Technologies Used

  • Languages: C++ 98, Makefile
  • Web Technologies: HTML, CSS
  • Additional Tools: Python
  • Programming Concepts: Object Oriented Programming, Non-block I/O, I/O Multiplexing (kqueue), HTTP/1.1

Implementation Details

Architecture

Configuration

http {
	error_page 404 500 413 ./www/error/default.html;
	index index.html ;
	autoindex on;
	server {
		server_name localhost:4000;
		listen  4000;
		location / {
			alias ./www/example/;
			limit_except POST GET DELETE;
			
			location /box/ {
				limit_except GET;
			}
		}
		location /abc/ {
			alias ./www/example/;
		}
	}
	server {
		listen 42;
		client_max_body_size 100;
		location / {
			alias ./www/example/;
		}
	}
}
conf_BU
A Bottom-up Approach
In the initialization process, a web server parses the configuration in a tree shape. This allows each location configuration to override specific settings like Nginx. Regex is partially implemented. (*, $)
This is implemented in Config.{hpp, cpp} in less than 100 lines.
conf_TD
A Top-down Approach
After making the tree, the configuration is being mapped into std::<map>(heap). This approach is to optimize the configuration access time of each HTTP request. The search is processed in several steps:
1. Port
2. Hostname
3. Location
By using this method, the access time is reduced from $O(N)$ to $O(log N)$.(We could have reduced it to O(1) if we used std::<unordered_map>(hashmap), but C++98 doesn't have that STL.)
This is implemented in ConfigMap.{hpp, cpp}

Event-Driven Architecture (I/O Multiplexing)

Flowchart flow_normal

This is how this program flows. Server, client sockets, and children’s PID are registered to kqueue.

class Server, Connection, CGI inherits class IEventHandler which has virtual void EventHandler(). When an event is caught, it calls EventHandler() thus calling the corresponding function in each class.

Server never falls into blocking I/O

  • Server can process multiple requests simultaneously: We used pipe() to communicate with CGI processes. Whenever the buffer is full, the server registers an event(EVFILT_WRITE) to kqueue and moves on to another event in kqueue
  • An idea from round-robin algorithm: Since we can queue events by using kqueue(epoll), the server slices its I/O. However, we did not set timeout to maximize throughput.

Key Features

  • HTTP/1.1 Compliant: Supports GET, POST, and DELETE methods, along with features like HTTP pipelining and cookies.
  • Nginx-like Configuration: Tree-shaped parsing for nested bracket {} configurations, resembling Nginx.
  • Efficient Configuration Access: A sophisticated mapping system reduces access time from O(N) to O(log N).
  • Unblocking I/O: Since the server should support multiple requests simultaneously, I/O never falls into blocking mode.

Challenges and Solutions

  • Class Destruction: Implemented smart pointers for efficient object management, adapting to C++ 98's limitations.
  • Forking and Multithreading: Refined our approach to meet project requirements, gaining insights into process creation and management in Linux.
  • Parsing Complexity: Developed efficient parsing strategies for both configuration files and HTTP requests. For configuration parsing, we've used bitmask to make the logic as simple as possible. For HTTP request paring, we used Nekipelov’s httpparser and modified it to fit our usage. That helped us a lot. Thank you!

Installation and Usage

Ensure you have a macOS environment and a C++ 98 compiler.

  1. Clone the repository: git clone [repository link]
  2. Compile the project: make re at the root directory.
  3. Run the server: ./webserv configs/example.conf
Screen Shot 2023-11-17 at 5 03 56 PM Screen Shot 2023-11-17 at 5 11 50 PM

|

Results and Discussion

This project was a deep dive into web server mechanics, leveraging C++ with OOP and I/O multiplexing. Our journey was marked by constant problem-solving and code refinement, enhancing our collaboration and communication skills. Despite some initial challenges, the final product is a testament to our perseverance and commitment to learning.

Acknowledgements

Nginx

GitHub - nginx/nginx

RFC 2616

RFC 2616: Hypertext Transfer Protocol -- HTTP/1.1

HTTP Parser

GitHub - nekipelov/httpparser: HTTP request, response and urls parser

Kernel Queue

Kernel Queue: The Complete Guide On The Most Essential Technology For High-Performance I/O

License

MIT License

webserv's People

Contributors

aprilistic avatar gcgang avatar gitubanana avatar labin97 avatar leechan02 avatar

Stargazers

 avatar  avatar  avatar

Forkers

gcgang labin97

webserv's Issues

Process type variable needed

프로세스 타입 정보 저장 필요, 시그널 핸들링 함수에서 프로세스 타입(Master, Worker) 별 시그널 핸들링 다름

CGI 블락될 수도 있는 이슈

문제점

waitpid(mPid, &status, 0); // 자식 프로세스가 종료될 때까지 대기

위 코드에서 만약에 CGI가 무한루프를 돈다면 -> 서버가 블락된다.

생각한 해결법

waitpid(mPid, &status, WNOHANG); // 잠깐 확인하고, 다시 돌아와서 확인하기

WNOHANG옵션을 쓰면, 자식 프로세스가 끝날 때까지 기다리지 않기 때문에, 블락을 피할 수 있다.
waitpid의 리턴값을 확인해서 끝났는지 확인하면 될 듯 싶다.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.