Acme Thttpd
To test, chdir to directory with files to be served before running<p><pre><code>    #ifndef IDX
    #define IDX 1
    #endif
    #if IDX==1 &#x2F;&#x2F; final
    
          #include&#x2F;*           micro HTTP server           *&#x2F;&lt;stdio.h&gt;
         #include&#x2F;*           usage: .&#x2F;http [port]          *&#x2F;&lt;stdlib.h&gt;
        #include&#x2F;*                                           *&#x2F;&lt;string.h&gt;
         #include&#x2F;*   the default port is 8080, files are   *&#x2F;&lt;unistd.h&gt;
          #include&#x2F;*    read from the current directory    *&#x2F;&lt;netdb.h&gt;
    
    
                      int main(int n,char**
                      V){int t=SOCK_STREAM,
                      N=SO_REUSEADDR,i=1,c=
                             htons(n
      &gt;1?atoi       (V[1]):  404*20)  ;struct sockaddr_in s
      ={000};       void*f=  &amp;s;char  *m,b[1036];s.sin_port
      =c;for(       N=!((t=  socket(  s.sin_family=AF_INET,
      #define       http(c)  setsock         ##opt(\
      t,SOL_S       ##OCKET  ,N,&amp;i,c         (i)),bi  ##nd(t,f,c(s))&lt;0)||
      t,0))&lt;0       ||(http  (sizeof         )listen  (t,5))&lt;&lt;10;N&amp;&amp;(0)&lt;=(c
      =accept(t,0,0));close  (c)){b[         n=recv(  c,b,N,0),0&gt;n?0:n]=0;n
      =!memcmp(b,&quot;GET &#x2F;&quot;,5)  &lt;&lt;6;for         (i=4;n^  &#x27;?&#x27;&amp;&amp;n&lt;       127&amp;&amp;n&gt;
      32;){n=b[++i];}strcpy  (b+i,b[         f=0,i-1  ]==&#x27;&#x2F;&#x27;?       &quot;index&quot;
      &quot;.html&quot;       :&quot;&quot;);m=  n?puts(         b+5),b[  03]=&#x27;.&#x27;       ,strstr
      (b,&quot;&#x2F;.&quot;       )||0==(  f=fopen         (&amp;b[3],  &quot;rb&quot;))?&quot;404 Not Foun&quot;
      &quot;d&quot;:&quot;2&quot;       &quot;00 OK&quot;                  :&quot;501 &quot;  &quot;Not Implemented&quot;;for
      ((send)       ((c),b,                  sprintf  (b,&quot;HTTP&#x2F;1.1 %s\r\n&quot;
      &quot;\r\n%&quot;       &quot;s&quot;,m,f                  ?&quot;&quot;:m),  0);f&amp;&amp;!
      ((send)       (c,b+0,                  fread(b  ,1,N,f)
                                                      ,0)-N&amp;&amp;
                                                      (fclose
                                                      (f),404
                                                      )););}}
    
    #elif IDX==2 &#x2F;&#x2F; almost readable
    
    #include &lt;stdio.h&gt;
    #include &lt;stdlib.h&gt;
    #include &lt;string.h&gt;
    #ifdef _WIN32 &#x2F;* needs linking with Ws2_32 *&#x2F;
    #include &lt;winsock2.h&gt;
    #define close closesocket
    #define strstr(a,b)strstr(a,b)||strchr(a,&#x27;:&#x27;)||strchr(a,&#x27;\\&#x27;)
    #define struct WSADATA w={0};WSAStartup(514,&amp;w);struct
    #else
    #include &lt;unistd.h&gt;
    #include &lt;netdb.h&gt;
    #endif
    int main(int N,char**V){
            int i=1,t,c,n;struct sockaddr_in s={0};
            char b[1036];const char*m;void*f;
            s.sin_port=htons(N&gt;1?atoi(V[1]):8080);
            for(N=(t=socket(s.sin_family=AF_INET,SOCK_STREAM,0))&lt;0||
                            (setsockopt(t,SOL_SOCKET,SO_REUSEADDR,f=&amp;i,sizeof(i)),
                            bind(t,f=&amp;s,sizeof(s)))||listen(t,5)?0:1024;
                            N&amp;&amp;(c=accept(t,f=0,0))&gt;=0;close(c)){
                    b[0&gt;(n=recv(c,b,N,0))?0:n]=0;
                    for(n=!memcmp(b,&quot;GET &#x2F;&quot;,i=5)&lt;&lt;6;&#x27;?&#x27;^n&amp;&amp;32&lt;n&amp;&amp;n&lt;127;)n=b[i++];
                    m=n?strcpy(b+i-1,b[i-2]-&#x27;&#x2F;&#x27;?&quot;&quot;:&quot;index.html&quot;),
                                    puts(b+5),b[3]=&#x27;.&#x27;,strstr(b,&quot;&#x2F;.&quot;)||!(f=fopen(b+3,&quot;rb&quot;))?
                                    &quot;404 Not Found&quot;:&quot;200 OK&quot;:&quot;501 Not Implemented&quot;;
                    for(send(c,b,sprintf(b,&quot;HTTP&#x2F;1.1 %s\r\n\r\n%s&quot;,m,f?&quot;&quot;:m),0);
                                    f&amp;&amp;!(send(c,b,fread(b,1,N,f),0)-N&amp;&amp;fclose(f)|N););
            }
    }
    
    #endif

</code></pre>
<a href="https:&#x2F;&#x2F;github.com&#x2F;ilyakurdyukov&#x2F;ioccc&#x2F;blob&#x2F;main&#x2F;practice&#x2F;2021.07-http&#x2F;prog.c" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;ilyakurdyukov&#x2F;ioccc&#x2F;blob&#x2F;main&#x2F;practice&#x2F;20...</a>