An HTTP server in a single .c file
Here&#x27;s one, small enough to fit in a comment:<p><pre><code>  #include &lt;stdio.h&gt;
  #include &lt;sys&#x2F;types.h&gt;
  #include &lt;sys&#x2F;socket.h&gt;
  #include &lt;netinet&#x2F;in.h&gt;

  main()
  {
        int rc;
        struct sockaddr_in ssa, csa;
        int ss, cs;

        memset(&amp;ssa, 0, sizeof ssa);
        ssa.sin_family = PF_INET;
        ssa.sin_port = htons(8080);
        ssa.sin_addr.s_addr = htonl(INADDR_ANY);

        ss = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
        bind(ss, (struct sockaddr*)&amp;ssa, sizeof ssa);
        listen(ss, 5);

        for (;;) {
                int ln;
                static char rq[1000];
                memset(&amp;csa, 0, sizeof csa);
                ln = sizeof(struct sockaddr);
                cs = accept(ss, (struct sockaddr*)&amp;csa, &amp;ln);
                memset(rq, 0, sizeof rq);
                recv(cs, rq, sizeof rq, 0);
               
                if (!strncmp(rq, &quot;GET &#x2F;&quot;, 5)) {
                        char *b;
                        FILE *f;
                        char *fn;
                        if (b = strchr(rq + 5, &#x27; &#x27;)) *b = 0;
                        else rq[sizeof rq - 1] = 0;
                        if (fn = malloc(strlen(rq + 5) + 5)) {
                            char *t, *type();
                            strcpy(fn, &quot;www&#x2F;&quot;);
                            strcat(fn, rq + 5);
                            if ((t = type(fn)) &amp;&amp; (f = fopen(fn, &quot;rt&quot;))) {
                                 int n;
                                 static char h[] = &quot;HTTP&#x2F;1.1 200 OK\nContent-Type: &quot;;
                                 static char c[1000];
                                 strcpy(c, h);
                                 strcat(c, t);
                                 strcat(c, &quot;\n\n&quot;);
                                 sendall(cs, c, strlen(c));
                                 while ((n = fread(c, 1, sizeof c, f)) &gt; 0) sendall(cs, c, n);
                                 fclose(f);
                            }
                            else {
                                 static char h[] = &quot;HTTP&#x2F;1.1 404 NotFound\nContent-Type: text&#x2F;html\n\n404 Nice try!&quot;;
                                 sendall(cs, h, sizeof h - 1);
                         }
                         free(fn);
                        }
                }
                close(cs);
        }
       
        close(ss);
        return 0;
  }

  sendall(s, c, n)
  char *c;
  {
        int m; for (m = 0; (m += send(s, c + m, n - m, 0)) &lt; n;);
  }

  char *type(fn)
  char *fn;
  {
      static char *s[] = {
          &quot;.html&quot;, &quot;text&#x2F;html&quot;,
          &quot;.asm&quot;, &quot;text&#x2F;plain&quot;,
          &quot;.c&quot;, &quot;text&#x2F;plain&quot;,
          &quot;.py&quot;, &quot;text&#x2F;plain&quot; };
      int i;
      for (i = 0; i &lt; sizeof s &#x2F; sizeof *s; i += 2)
          if (strstr(fn, s[i]))
              return s[i + 1];
      return 0;
  }</code></pre>