Skip to content

menasy/WebServer

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🌐 WebServ - C++98 HTTP Web Server

Web Server Architecture

WebServ Logo

C++ License Build Status

A high-performance, non-blocking HTTP/1.1 web server implementation in C++98


📖 Proje Hakkında

WebServ, ekip arkadaşımla birlikte C++98 dilinde sıfırdan geliştirdiğimiz kendi HTTP/1.1 web sunucumuzdur. Nginx benzeri olay tabanlı mimari (event-driven architecture) ile tek bir işlemde birden fazla bağlantıyı eşzamanlı olarak yönetebilen, socket programlama kullanarak tek thread üzerinde çalışan yüksek performanslı bir yapı tasarladık.

Non-blocking I/O mekanizmaları sayesinde çoklu istemci bağlantılarını etkin şekilde yöneten sunucumuz, çoklu port desteği, dinamik içerik işleme ve özelleştirilebilir yönlendirme özellikleriyle modern web sunucusu ihtiyaçlarını karşılamaktadır. Bu proje, ağ programlama, HTTP protokolü ve sistem kaynaklarının optimum kullanımı konularında derinlemesine teknik deneyim sağladı.

👥 Takım

Bu proje, 42 İstanbul öğrencileri tarafından ortak bir çalışma ile hayata geçirilmiştir:

Developer GitHub Profile
menasy GitHub
ekose GitHub

🎯 Proje Hedefleri

  • HTTP/1.1 Protokolü: RFC 2616 standardına uygun HTTP sunucusu
  • Non-blocking I/O: Yüksek performans için asenkron soket programlama
  • CGI Desteği: Python, PHP ve diğer betik dilleri için CGI arayüzü
  • Çoklu Server: Tek bir yapılandırma dosyasında birden fazla virtual host
  • Modüler Mimari: Genişletilebilir ve sürdürülebilir kod yapısı

🌐 Web Server Nedir?

Web sunucusu, HTTP (Hypertext Transfer Protocol) protokolü üzerinden web sayfalarını, dosyaları ve diğer web içeriklerini istemcilere (tarayıcılar, uygulamalar) sunan bir yazılım uygulamasıdır.

🔄 Çalışma Prensibi

 ┌─────────────┐    HTTP İsteği     ┌─────────────┐
 │   İstemci   │ ──────────────────► │ Web Sunucu  │
 │ (Tarayıcı)  │                    │  (WebServ)  │
 │             │ ◄────────────────── │             │
 └─────────────┘    HTTP Yanıtı     └─────────────┘

Server-Client Architecture HTTP Server-Client İletişim Mimarisi

Temel İşlevler:

  • 🌍 HTTP isteklerini kabul etme ve işleme
  • 📄 Statik dosyaları sunma (HTML, CSS, JS, resimler)
  • 🔧 Dinamik içerik üretimi (CGI ile)
  • 🛡️ Güvenlik kontrolü ve erişim yönetimi
  • 📊 İstek/yanıt loglaması

🚀 HTTP Protokolü ve İmplementasyon

📡 HTTP Nedir?

HTTP (Hypertext Transfer Protocol), World Wide Web'in temelini oluşturan uygulama katmanı protokolüdür. İstemci-sunucu modeline dayalı, durumsuz (stateless) bir protokoldür.

🔧 WebServ'de Desteklenen HTTP Özellikleri

✅ HTTP Metodları

  • GET: Kaynak alma istekleri
  • POST: Veri gönderme (form, dosya yükleme)
  • DELETE: Kaynak silme istekleri

📋 HTTP Status Kodları

200 OK              // Başarılı istek
301/302 Redirect    // Yönlendirme
400 Bad Request     // Hatalı istek
403 Forbidden       // Erişim reddedildi
404 Not Found       // Kaynak bulunamadı
405 Method Not Allowed  // Metod desteklenmiyor
413 Entity Too Large    // Çok büyük istek
500 Internal Server Error // Sunucu hatası

🗂️ HTTP Headers

  • Content-Type: MIME türü belirleme
  • Content-Length: İçerik boyutu
  • Connection: Bağlantı yönetimi (keep-alive)
  • Host: Virtual host desteği
  • Transfer-Encoding: Chunked transfer

🏗️ Mimari ve Tasarım

📊 Sistem Mimarisi

┌─────────────────────────────────────────────────────┐
│                   WebServ Mimarisi                  │
├─────────────────────────────────────────────────────┤
│  main.cpp (Entry Point)                            │
├─────────────────────────────────────────────────────┤
│  CheckConfig (Yapılandırma Doğrulama)              │
├─────────────────────────────────────────────────────┤
│  WebServer (Ana Sunucu Sınıfı)                     │
│  ├── Poll-based Event Loop                         │
│  ├── Client Connection Management                  │
│  └── Request/Response Handling                     │
├─────────────────────────────────────────────────────┤
│  HttpRequest (HTTP Parser)                         │
│  ├── Header Parsing                                │
│  ├── Body Parsing                                  │
│  └── URL Decoding                                  │
├─────────────────────────────────────────────────────┤
│  FileHandler (Dosya İşlemleri)                     │
│  ├── Static File Serving                           │
│  ├── Directory Listing                             │
│  └── CGI Execution                                 │
├─────────────────────────────────────────────────────┤
│  MethodTransaction (HTTP İşlemleri)                │
│  ├── GET Handler                                   │
│  ├── POST Handler                                  │
│  └── DELETE Handler                                │
├─────────────────────────────────────────────────────┤
│  ServerConf / LocationConf (Yapılandırma)          │
└─────────────────────────────────────────────────────┘

🔄 Non-blocking I/O ile Event Loop

WebServ, performans optimizasyonu için poll() sistem çağrısı ile non-blocking I/O kullanır:

Event-Driven Architecture Event-Driven Architecture ve Non-blocking I/O Yapısı

// Ana event loop
while (!g_signal) {
    int result = poll(pollVec.data(), pollVec.size(), TIMEOUT);
    
    for (size_t i = 0; i < pollVec.size(); i++) {
        if (pollVec[i].revents & POLLIN) {
            // Yeni veri geldi - read
        }
        if (pollVec[i].revents & POLLOUT) {
            // Yazma hazır - send
        }
    }
}

Avantajları:

  • 🚀 Yüksek eşzamanlılık (binlerce bağlantı)
  • 💾 Düşük bellek kullanımı
  • ⚡ Hızlı yanıt süreleri
  • 🔄 Engellemeyen I/O işlemleri

🔌 Socket Programlama

🌐 TCP Socket Oluşturma

WebServ, Berkeley Sockets API kullanarak TCP bağlantıları yönetir:

// Socket oluşturma
int serverSocket = socket(AF_INET, SOCK_STREAM, 0);

// Non-blocking yapma
int flags = fcntl(serverSocket, F_GETFL, 0);
fcntl(serverSocket, F_SETFL, flags | O_NONBLOCK);

// Adres bağlama
struct sockaddr_in serverAddr;
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
serverAddr.sin_port = htons(8080);

bind(serverSocket, (struct sockaddr*)&serverAddr, sizeof(serverAddr));
listen(serverSocket, SOMAXCONN);

🔄 Bağlantı Yaşam Döngüsü

1. Socket Creation     → socket()
2. Bind Address        → bind()
3. Listen             → listen()
4. Accept Connection  → accept()
5. Read Request       → recv()
6. Process Request    → HTTP parsing
7. Send Response      → send()
8. Close Connection   → close()

🛠️ CGI (Common Gateway Interface)

🔧 CGI Nedir?

CGI, web sunucularının harici programları çalıştırarak dinamik içerik üretmesini sağlayan standart bir arayüzdür.

🐍 WebServ'de CGI Desteği

WebServ aşağıdaki CGI özelliklerini destekler:

# Desteklenen betik türleri
.py  → Python3
.php → PHP
.pl  → Perl

🌍 CGI Environment Variables

// CGI için set edilen ortam değişkenleri
REQUEST_METHOD=GET
SCRIPT_NAME=/cgi-bin/script.py
PATH_INFO=/extra/path
QUERY_STRING=param=value
CONTENT_TYPE=application/x-www-form-urlencoded
CONTENT_LENGTH=42
SERVER_NAME=localhost
SERVER_PORT=8080
HTTP_HOST=localhost:8080

🔄 CGI Execution Flow

1. HTTP İsteği gelir
2. CGI betiği tespit edilir
3. Environment variables set edilir
4. fork() ile child process oluşturulur
5. execve() ile betik çalıştırılır
6. Pipe ile output okunur
7. HTTP response olarak gönderilir

⚙️ Kurulum ve Kullanım

🔧 Yapılandırma Örneği

server {
    listen 127.0.0.1:8080;
    server_name localhost;
    
    client_max_body_size 2M;
    root var/www;
    index index.html;
    
    access_log logs/access.log;
    error_log logs/error.log;
    
    # Ana lokasyon
    location / {
        methods GET POST DELETE;
        autoindex on;
        try_files $uri $uri/ =404;
    }
    
    # CGI lokasyonu
    location /cgi-bin/ {
        methods GET POST;
        cgi_ext .py /usr/bin/python3;
        cgi_ext .php /usr/bin/php;
    }
    
    # Yönlendirme
    location /redirect/ {
        return 302 https://github.com;
    }
    
    # Dosya yükleme
    location /upload/ {
        methods POST;
        client_max_body_size 10M;
    }
    
    # Hata sayfaları
    error_page 404 /errors/404.html;
    error_page 500 /errors/500.html;
}

🔨 Derleme

# Projeyi klonlayın
git clone https://github.com/e-kose/WebServer.git
cd WebServer

# Derleyin
make

🚀 Çalıştırma

# Temel kullanım
./webserv config/conf.conf

Sunucu başlatıldıktan sonra tarayıcınızdan aşağıdaki adrese giderek test edebilirsiniz:

http://localhost:8080/

Chrome, Firefox, Safari gibi herhangi bir web tarayıcısından bu adrese istek göndererek WebServ sunucusunun çalıştığını doğrulayabilirsiniz.

📋 Gereksinimler

  • Derleyici: g++ veya clang++ (C++98 uyumlu)
  • İşletim Sistemi: Linux/macOS
  • Python: CGI desteği için Python 3.x
  • Make: Build sistemi için

🧪 Test ve Örnek Kullanım

🔍 Temel HTTP Testleri

WebServ sunucusu, kullanıcı dostu web arayüzü üzerinden kolayca test edilebilir. Ana sayfa (index.html) üzerinden tüm HTTP metodları ve özellikler interaktif olarak deneyimlenebilir.

WebServ Ana Sayfa WebServ ana sayfa arayüzü - tüm özellikler web üzerinden test edilebilir

📊 Performans Testi

# Apache Bench ile yük testi
ab -n 1000 -c 50 http://localhost:8080/

# Siege ile stress test
siege -c 100 -t 60s http://localhost:8080/

🐛 Debug ve Monitoring

# Bağlantı durumu
netstat -an | grep :8080

# Process izleme
ps aux | grep webserv

# Memory kullanımı
valgrind --leak-check=full ./webserv config/conf.conf

📁 Proje Yapısı

WebServer/
├── 📁 config/
│   └── conf.conf                 # Sunucu yapılandırması
├── 📁 include/                   # Header dosyaları
│   ├── WebServer.hpp            # Ana sunucu sınıfı
│   ├── HttpRequest.hpp          # HTTP parser
│   ├── ServerConf.hpp           # Server yapılandırması
│   ├── LocationConf.hpp         # Location yapılandırması
│   ├── FileHandler.hpp          # Dosya işlemleri
│   ├── MethodTransaction.hpp    # HTTP metodları
│   ├── CheckConfig.hpp          # Config doğrulama
│   ├── Parser.hpp               # Config parser
│   ├── Tokenizer.hpp            # Lexical analyzer
│   └── HelperClass.hpp          # Yardımcı fonksiyonlar
├── 📁 src/                      # Kaynak kodlar
│   ├── WebServer.cpp            # Ana sunucu implementasyonu
│   ├── HttpRequest.cpp          # HTTP parsing logic
│   ├── ServerConf.cpp           # Server config handling
│   ├── LocationConf.cpp         # Location config handling
│   ├── FileHandler.cpp          # File operations
│   ├── MethodTransaction.cpp    # HTTP methods implementation
│   ├── CheckConfig.cpp          # Configuration validation
│   ├── Parser.cpp               # Configuration parsing
│   ├── Tokenizer.cpp            # Lexical analysis
│   └── HelperClass.cpp          # Utility functions
├── 📁 var/www/                  # Web root directory
│   ├── index.html               # Ana sayfa
│   ├── 📁 cgi-bin/              # CGI betikleri
│   │   ├── upload.py            # Dosya yükleme
│   │   ├── getScript.py         # GET test script
│   │   └── sendScript.py        # Response test script
│   ├── 📁 errors/               # Hata sayfaları
│   │   ├── 400.html             # Bad Request
│   │   ├── 403.html             # Forbidden
│   │   ├── 404.html             # Not Found
│   │   └── 500.html             # Internal Server Error
│   └── 📁 static/               # Statik dosyalar
│       ├── 📁 css/              # Stil dosyaları
│       ├── 📁 js/               # JavaScript
│       └── 📁 images/           # Resimler
├── 📁 logs/                     # Log dosyaları
│   ├── access.log               # Erişim logları
│   └── error.log                # Hata logları
├── main.cpp                     # Ana program entry point
├── Makefile                     # Build sistemi
└── README.md                    # Bu dosya

🔧 Özellikler ve Yetenekler

✅ HTTP/1.1 Özellikleri

  • GET, POST, DELETE metodları
  • HTTP/1.1 protokol desteği
  • Keep-Alive bağlantılar
  • Chunked Transfer Encoding
  • Virtual Host (server_name) desteği
  • Custom Error Pages
  • Request/Response Headers
  • URL Decoding
  • Query String parsing

🛡️ Güvenlik Özellikleri

  • Path Traversal koruması
  • File Permission kontrolü
  • Client Body Size sınırı
  • Input Validation
  • Safe CGI Execution
  • Access Control

📊 Performans Özellikleri

  • Non-blocking I/O (poll)
  • Event-driven mimari
  • Efficient Memory kullanımı
  • Connection Pooling
  • Fast Static File serving
  • Optimized HTTP parsing

🔧 CGI ve Dinamik İçerik

  • Python CGI desteği
  • PHP CGI desteği
  • Environment Variables
  • POST Data handling
  • File Upload via CGI
  • Safe Process execution

📚 Referanslar ve Kaynaklar

📖 HTTP ve Web Sunucu Temelleri

🛠️ 42 Webserv ve C++ Programlama

🔌 Socket Programlama

🔧 CGI ve Web Geliştirme

🎥 Video Kaynakları


🤝 Katkı ve Geliştirme


🎯 Sonuç

WebServ, modern web teknolojilerinin temellerini anlamak ve sistem programlama becerilerini geliştirmek için kapsamlı bir öğrenme deneyimi sunar. Proje, HTTP protokolünden socket programlamaya, CGI implementasyonundan performans optimizasyonuna kadar web sunucu geliştirmenin önemli konularını kapsar.

🎓 Edinilen Kritik Yetenekler

Sistem Seviyesi Programlama:

  • 🔧 Berkeley Sockets API ile düşük seviyeli ağ programlama mastery'si
  • ⚙️ POSIX sistem çağrıları ve kernel interface anlayışı
  • 🧠 Memory management ve resource lifecycle yönetimi
  • 🔄 Process/thread yönetimi ve IPC (Inter-Process Communication)

Web Teknolojileri ve Protokoller:

  • 🌐 HTTP/1.1 protokolünün RFC standartlarına uygun implementasyonu
  • 📡 TCP/IP stack üzerinde gerçek zamanlı veri iletişimi
  • � Web güvenliği, input validation ve attack prevention
  • 📋 MIME types, content negotiation ve HTTP headers mastery'si

Performance Engineering:

  • ⚡ Event-driven architecture ve non-blocking I/O patterns
  • 📊 Scalable server design ve concurrent connection handling
  • 💾 Memory-efficient data structures ve algorithm optimization
  • 🔄 Load balancing concepts ve high-availability principles

DevOps ve Sistem Yönetimi:

  • �️ Configuration management ve deployment strategies
  • 📈 Monitoring, logging ve debugging techniques
  • 🔍 Performance profiling ve bottleneck analysis
  • 🚀 Production-ready code ve enterprise-level error handling

Bu kapsamlı geliştirme süreci, web teknolojilerinden sistem programlamaya, ağ mimarisinden performans optimizasyonuna kadar geniş bir spektrumda derin teknik bilgi ve deneyim kazandırmıştır. Proje tamamlandığında, modern web altyapısının işleyişini kavrayabilme ve bu tür kompleks sistemleri sıfırdan tasarlayıp uygulayabilme yetkinliği elde edilmiş olmaktadır.


⭐ Bu projeyi beğendiyseniz star vermeyi unutmayın!

🔝 Başa Dön

About

C++ ile sıfırdan geliştirilmiş HTTP/1.1 web sunucusu.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C++ 73.3%
  • CSS 12.8%
  • HTML 7.8%
  • Python 3.6%
  • JavaScript 1.6%
  • Makefile 0.9%