<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Petersburg Awinbe-Yisazuah</title>
    <description>The latest articles on DEV Community by Petersburg Awinbe-Yisazuah (@peterzzburg).</description>
    <link>https://dev.to/peterzzburg</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2818911%2Fd079f1d3-042a-401f-888c-417c83240e38.png</url>
      <title>DEV Community: Petersburg Awinbe-Yisazuah</title>
      <link>https://dev.to/peterzzburg</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/peterzzburg"/>
    <language>en</language>
    <item>
      <title>Doing Hard Things:An HTTP Server From Scratch</title>
      <dc:creator>Petersburg Awinbe-Yisazuah</dc:creator>
      <pubDate>Thu, 13 Mar 2025 18:01:34 +0000</pubDate>
      <link>https://dev.to/peterzzburg/doing-hard-things-2h2</link>
      <guid>https://dev.to/peterzzburg/doing-hard-things-2h2</guid>
      <description>&lt;p&gt;The craze of AI taking the jobs of developers really got me thinking about how i need to solidify my understanding of fundamental concepts so over the weekend i decided to build an HTTP server from scratch.&lt;/p&gt;

&lt;h2&gt;
  
  
  WHAT IS HTTP AND HOW DOES IT WORK
&lt;/h2&gt;

&lt;p&gt;The HTTP(S) protocol is an application layer protocol which is stateless and is relies on TCP protocol for data transmission. When we say stateless we simply mean that a persistent connection between devices communicating is not maintained.The connection is broken after every data transfer.At the foundation of every HTTP server is a TCP server listening in for incoming connections and accepting. This implies that HTTP server is a wrapper around a TCP server. Based on this we can break down an HTTP server into its essential components and attempt building each of them and then finally putting them together&lt;/p&gt;

&lt;h3&gt;
  
  
  Main components of the HTTP server
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Base TCP server&lt;/li&gt;
&lt;li&gt;Request handler&lt;/li&gt;
&lt;li&gt;Request processor&lt;/li&gt;
&lt;li&gt;Response handler&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd5gzc0gfjhf7qjd9s7su.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd5gzc0gfjhf7qjd9s7su.png" alt="Image description" width="800" height="221"&gt;&lt;/a&gt;&lt;br&gt;
Now lets pick each part and explore it.&lt;br&gt;
&lt;strong&gt;Base TCP server:&lt;/strong&gt;&lt;br&gt;
The TCP server is a socket bound to a specific port and IP address, listening for incoming connections and deciding whether to accept or reject them.Most servers can handle concurrent connections and so I have to build the TCP server to handle connections concurrently.To handle this I took inspiration from the way databases handle connections concurrently with thread pools. For every accepted connection i spin up a worker thread to handle communications&lt;br&gt;
&lt;strong&gt;Request Handler:&lt;/strong&gt;&lt;br&gt;
The request handler component i built extracts the necessary information from the request data and passes on to the processor component. For simplicity sake my focus was on serving static files. HTTP data is binary encoded and has three components; the request line,the request headers and the request body depending on the request method respectively as show below   &lt;/p&gt;

&lt;p&gt;example GET&lt;br&gt;
&lt;code&gt;GET / HTTP/1.1&lt;br&gt;
Host: localhost:8080&lt;br&gt;
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:135.0) Gecko/20100101 Firefox/135.0&lt;br&gt;
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;example POST&lt;br&gt;
&lt;code&gt;POST /api/login HTTP/1.1\r\n\&lt;br&gt;
Host: localhost:8080\r\n\&lt;br&gt;
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:135.0) Gecko/20100101 Firefox/135.0\r\n\&lt;br&gt;
Content-Type: application/json\r\n\&lt;br&gt;
Content-Length: 44\r\n\&lt;br&gt;
Accept: */*\r\n\&lt;br&gt;
Connection: keep-alive\r\n\&lt;br&gt;
\r\n\&lt;br&gt;
{"username": "test_user", "password": "12345"}\r\n&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;My request handler parses the data and retrieves the request line. The request line is also parsed to retrieve the request method and the path. I then pass both the request method and and path to my request processor.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Request processor&lt;/strong&gt;&lt;br&gt;
My request processor locates the HTML file requested and encodes it in binary format. The encoded HTML file is then passed to the request handler&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Response handler&lt;/strong&gt;&lt;br&gt;
My response handler receives the encoded HTML data and constructs the response in the standard HTTP response format &lt;br&gt;
This is how a sample response will look like&lt;br&gt;
&lt;code&gt;HTTP/1.1 200 OK\r\n&lt;br&gt;
Date: Sun, 09 Mar 2025 12:34:56 GMT\r\n&lt;br&gt;
Content-Type: text/html\r\n&lt;br&gt;
Content-Length: 125\r\n&lt;br&gt;
Connection: keep-alive\r\n&lt;br&gt;
Server: nginx/1.18.0\r\n&lt;br&gt;
\r\n&lt;br&gt;
&amp;lt;!DOCTYPE html&amp;gt;&lt;br&gt;
&amp;lt;html lang="en"&amp;gt;&lt;br&gt;
&amp;lt;head&amp;gt;&lt;br&gt;
    &amp;lt;meta charset="UTF-8"&amp;gt;&lt;br&gt;
    &amp;lt;title&amp;gt;Example Page&amp;lt;/title&amp;gt;&lt;br&gt;
&amp;lt;/head&amp;gt;&lt;br&gt;
&amp;lt;body&amp;gt;&lt;br&gt;
    &amp;lt;h1&amp;gt;Hello, World!&amp;lt;/h1&amp;gt;&lt;br&gt;
&amp;lt;/body&amp;gt;&lt;br&gt;
&amp;lt;/html&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Finally the base TCP server closes the connection and the communication is ended.&lt;br&gt;
Access the code base for this project &lt;a href="https://github.com/R0hi77/pulse" rel="noopener noreferrer"&gt;here&lt;/a&gt; &lt;/p&gt;

</description>
      <category>programming</category>
    </item>
    <item>
      <title>What Happens Behind the Scenes of an HTTP Request?</title>
      <dc:creator>Petersburg Awinbe-Yisazuah</dc:creator>
      <pubDate>Mon, 10 Feb 2025 00:49:21 +0000</pubDate>
      <link>https://dev.to/peterzzburg/what-happens-behind-the-scenes-of-an-http-request-1jcc</link>
      <guid>https://dev.to/peterzzburg/what-happens-behind-the-scenes-of-an-http-request-1jcc</guid>
      <description>&lt;p&gt;When you type a URL like &lt;a href="https://google.com" rel="noopener noreferrer"&gt;google.com&lt;/a&gt; into your browser and hit Enter, a fascinating chain of events is set into motion. A request is sent to a server, and within seconds—thanks to a decent internet connection—you see the familiar white page with "Google" boldly written and a search bar in the center. But what exactly happens between hitting Enter and seeing the webpage? Let’s dive into the intricate processes that make this possible.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: DNS Resolution – Finding the Server’s Address
&lt;/h2&gt;

&lt;p&gt;Before your browser can fetch the webpage, it needs to figure out where Google’s server is located. This process is known as Domain Name System (DNS) resolution.&lt;br&gt;
What is DNS Resolution?&lt;br&gt;
Computers don’t inherently understand human-readable domain names like google.com. Instead, they use IP addresses to locate servers on the internet. An IP address is a unique identifier for a computer or device on a network. Domain names are simply mnemonics to make it easier for humans to access websites without needing to remember complex numerical addresses. DNS acts like the internet’s phonebook, mapping domain names to their corresponding IP addresses so computers can locate web servers. For example, google.com might map to an IP address like &lt;code&gt;216.58.223.196&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How Does DNS Resolution Work?&lt;/strong&gt;&lt;br&gt;
When you enter google.com in your browser, the browser first tries to find its matching IP address. This is typically done in the following steps:&lt;br&gt;
a. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Checking the Local Cache&lt;/strong&gt;&lt;br&gt;
The browser first checks its local DNS cache to see if it has a recent record of google.com's IP address. If found, the browser uses this cached IP and proceeds with the request, saving time.&lt;br&gt;
b. &lt;strong&gt;Querying the Operating System’s Resolver&lt;/strong&gt;&lt;br&gt;
If the browser cache doesn’t have the IP, it makes a system call to the local DNS resolver, which is managed by the operating system. The OS resolver then checks its own cache. If it finds a matching record, it returns the IP to the browser.&lt;br&gt;
Fun fact: To see the cached DNS records on a Linux system, you can use the following command: &lt;code&gt;sudo resolvectl query google.com&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffr7xj2cqczbtpuf4tgss.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffr7xj2cqczbtpuf4tgss.png" alt="terminal output" width="800" height="108"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;c. &lt;strong&gt;Requesting from an External DNS Server&lt;/strong&gt;&lt;br&gt;
If no cached record exists, the local resolver queries an external DNS server, usually provided by your ISP or a public DNS provider like Google DNS &lt;code&gt;(8.8.8.8)&lt;/code&gt; or Cloudflare DNS &lt;code&gt;(1.1.1.1)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;d. &lt;strong&gt;Navigating the DNS Hierarchy&lt;/strong&gt;&lt;br&gt;
DNS servers are structured in a hierarchy, and the request travels through multiple layers:&lt;br&gt;
Root DNS Servers: These servers direct the request to the appropriate Top-Level Domain (TLD) server (e.g., .com for google.com, .org for other domains, etc.).&lt;br&gt;
TLD Servers: The .com TLD server then directs the request to the Authoritative Name Server for google.com.&lt;br&gt;
Authoritative DNS Server: This server holds the actual IP address for google.com and returns it to the resolver.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frval6zgts22m7zp190bu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frval6zgts22m7zp190bu.png" alt="DNS Hierarchy" width="800" height="475"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;e. &lt;strong&gt;Returning the IP Address&lt;/strong&gt;&lt;br&gt;
Once the resolver obtains the IP address, it sends it back to the browser. The browser can now initiate the actual HTTP request to Google’s server using the GET method.&lt;/p&gt;

&lt;p&gt;By completing this process, the browser now knows exactly where to send the request and can proceed to retrieve the webpage. But the journey doesn’t end here—next comes establishing a connection to the server.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: The Three-Way TCP Handshake – Establishing a Connection
&lt;/h2&gt;

&lt;p&gt;At this point, you might be wondering why we can’t just receive the response from Google’s server immediately. It’s important to keep in mind that the data requested from servers isn’t always simple or small. Often, it includes additional elements like images, videos, and scripts. To ensure smooth communication and prevent data loss, both the server and your computer need to establish a reliable connection. This process is known as the &lt;strong&gt;three-way TCP handshake&lt;/strong&gt; and can be thought of as a preparation stage to ensure both parties are ready for communication.&lt;br&gt;
Here’s how it works:&lt;br&gt;
&lt;strong&gt;SYN Packet&lt;/strong&gt;&lt;br&gt;
The client (your browser) sends a &lt;code&gt;SYN&lt;/code&gt; (synchronize) packet to Google’s server, signaling that it’s ready to initiate communication.&lt;br&gt;
&lt;strong&gt;SYN-ACK Packet&lt;/strong&gt;&lt;br&gt;
Google’s server responds with a &lt;code&gt;SYN-ACK&lt;/code&gt; (synchronize-acknowledge) packet, acknowledging the receipt of the &lt;code&gt;SYN&lt;/code&gt; packet and indicating its readiness to communicate.&lt;br&gt;
&lt;strong&gt;ACK Packet&lt;/strong&gt;&lt;br&gt;
Finally, the client sends an &lt;code&gt;ACK&lt;/code&gt; (acknowledge) packet to Google’s server, confirming that the &lt;code&gt;SYN-ACK&lt;/code&gt; packet was received. At this point, the connection is fully established, and data transfer can begin.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: Fetching and Rendering the Webpage
&lt;/h2&gt;

&lt;p&gt;Once the connection is established, Google’s server sends back the requested homepage. This typically includes HTML, CSS, JavaScript, and other resources like images or videos. Your browser then processes these files, renders the page, and displays it as you’re used to seeing.&lt;br&gt;
And just like that, within seconds, the familiar Google homepage appears in your browser, ready for you to start searching.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;What seems like a simple action—typing a URL and hitting Enter—involves a complex series of steps working seamlessly behind the scenes. From DNS resolution to the TCP three-way handshake, each step plays a crucial role in delivering the webpage to your screen. The next time you browse the web, you’ll have a deeper appreciation for the intricate processes that make it all possible&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>backend</category>
      <category>web</category>
    </item>
  </channel>
</rss>
