Skip to content

Piyush-Goenka/HTTP-Server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

Simple HTTP Server Project

What is this?

This is my HTTP server for a college project. It’s a simple web server made with Python that handles GET and POST requests, serves files like HTML, images, and text, and saves JSON data. It uses threads to handle multiple users at the same time.

How to Run

  1. Make sure you have Python 3 installed.

  2. Save the code in a file called http_server.py.

  3. Create a folder named stuff in the same place as the code.

  4. Put some files in stuff:

    • At least 3 HTML files (like index.html, about.html, contact.html).
    • At least 2 PNG images (one should be bigger than 1MB).
    • At least 2 JPEG images.
    • At least 2 text files (.txt).
  5. Open a terminal and go to the folder with the code.

  6. Run the server with:

    python http_server.py
    

    You can also add options:

    • python http_server.py 8000 to use a different port.
    • python http_server.py 8000 0.0.0.0 to set host.
    • python http_server.py 8000 0.0.0.0 20 to set number of threads.
  7. Open a browser and go to http://localhost:8080 to see it work!

  8. To stop, press Ctrl+C.

What it Does

  • GET Requests:
    • Shows HTML files (like index.html for /).
    • Downloads images (PNG, JPEG) and text files as files.
    • Only allows .html, .txt, .png, .jpg, .jpeg.
  • POST Requests:
    • Takes JSON data at /upload and saves it as a file in stuff/uploads.
    • Files are named like upload_20231010_123456_abcd.json.
  • Security:
    • Blocks bad paths (like ../) to keep files safe.
    • Checks the Host header to avoid bad requests.
    • Only accepts JSON for POST.
  • Threads: Uses 10 threads by default to handle many users. Queues extra connections if busy.
  • Connections: Supports keep-alive for HTTP/1.1, closes after 30 seconds or 100 requests.

Folder Setup

project/
├── http_server.py
├── stuff/
│   ├── index.html
│   ├── about.html
│   ├── contact.html
│   ├── sample.txt
│   ├── logo.png
│   ├── photo.jpg
│   └── uploads/ (for POST files)

Testing

  • Basic Tests:
    • Visit http://localhost:8080 to see index.html.
    • Try /about.html or /logo.png to download files.
    • Send JSON to /upload using a tool like Postman.
    • Try a bad file like /something.exe (should fail with 415).
  • Security Tests:
    • Try /../etc/passwd (should get 403).
    • Send a request with wrong Host (should get 403).
  • Concurrency:
    • Open multiple browser tabs to download files at once.
    • Test with big files (>1MB) to check they download fine.
  • Testing with Telnet: You can use telnet to test the server manually:
    1. Start the server (python http_server.py).
    2. Open a terminal and type:
      telnet localhost 8080
      
    3. Type an HTTP request (hit Enter twice at the end):
      GET /index.html HTTP/1.1
      Host: localhost:8080
      
      
      • Should return the HTML content of index.html.
    4. Try a file download:
      GET /logo.png HTTP/1.1
      Host: localhost:8080
      
      
      • Should return binary data with headers like Content-Type: application/octet-stream.
    5. Test a bad path:
      GET /../etc/passwd HTTP/1.1
      Host: localhost:8080
      
      
      • Should return 403 Forbidden.
    6. Test a wrong host:
      GET /index.html HTTP/1.1
      Host: evil.com
      
      
      • Should return 403 Forbidden.
    7. Test a POST request (harder with telnet, better with Postman):
      POST /upload HTTP/1.1
      Host: localhost:8080
      Content-Type: application/json
      Content-Length: 27
      
      {"test":"hello"}
      
      • Should return 201 Created with a JSON response.

Limitations

  • Only supports GET and POST (no PUT, DELETE, etc.).
  • Only allows specific file types (html, txt, png, jpg).
  • JSON uploads can’t be bigger than 5MB.
  • If too many people connect, some might get a “busy” error (503).

Notes

  • Files are read in binary mode to keep them safe.
  • Uses 8KB chunks for sending files to make it fast.
  • Logs everything with timestamps to see what’s happening.
  • Code is on my GitHub: [insert your GitHub link here].

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors