g | x | w | all
Bytes Lang Time Link
373Rust240221T154747Zjan
408C gcc lws2_32 + winsock2.h240325T001436ZConor O&
151Python + Flask231130T024923Zlyxal
052JavaScript Deno231201T023803Znoodle p
149PowerShell Core231203T222406ZJulian
199Racket #lang webserver/insta231203T195206ZLiberalA
106Bash + ncat from nmap231202T140511Zcorvus_1
095Python 3 + http.server231130T043815ZATaco
176Python 3231130T034605Zgsitcia
138Lua + LuaSockets231130T033121ZATaco
077Javascript Node.js231130T030938Zl4m2

Rust, 385 373 bytes

use{std::*,io::*};fn main(){write!(net::TcpListener::bind("[::1]:8080").unwrap().accept().unwrap().0,r"HTTP/1.1 200
Content-Type:text/plain
Content-Length:373

{1}{1}{}","\x22#);}",r#"use{std::*,io::*};fn main(){write!(net::TcpListener::bind("[::1]:8080").unwrap().accept().unwrap().0,r"HTTP/1.1 200
Content-Type:text/plain
Content-Length:373

{1}{1}{}","\x22#);}",r#""#);}

When run as the main.rs file in a cargo project with cargo run, this program waits until you go to [::1]:8080 (ipv6 loopback) in your browser (this is what accept() does) and then sends over its source code and ends.

C (gcc -lws2_32 + winsock2.h), 408 bytes

Z;main(){char*S,Q[16]={2,0,4,1},*M=malloc(999);WSAStartup(9,M);sprintf(M,S="HTTP/1.1 200 OK%c%cContent-Length: 408%c%c%c%cZ;main(){char*S,Q[16]={2,0,4,1},*M=malloc(999);WSAStartup(9,M);sprintf(M,S=%c%s%c,13,10,13,10,13,10,34,S,34);bind(Z=socket(2,1,0),Q,16);for(listen(Z,3);;)send(accept(Z,Q,0),M,448,0);}",13,10,13,10,13,10,34,S,34);bind(Z=socket(2,1,0),Q,16);for(listen(Z,3);;)send(accept(Z,Q,0),M,448,0);}

This uses the Windows Socket 2 API. I have made many questionable choices for the sake of the golf, so there is likely some instability. Listens to an arbitrary number of HTTP requests on port 1025. Compiled with gcc server.c -o server -lws2_32 (e.g.).

Verification

In Firefox:

A picture of the Quine in action on Firefox, showing the full code from earlier.

[![A picture of the Quine in action on Firefox, showing the full code from earlier.][2]][2]

Here is the Ruby test harness I used to verify integrity:

require "net/http"
puts "Waiting for server to start..."
server_process = IO.popen("server.exe") { |server|
    3.times { |i|
        response = Net::HTTP.get_response(URI("http://localhost:1025")).body
        puts "Response ##{i} obtained:"
        puts response
        
        puts "Same as server.c?"
        puts File.read("server.c") == response
    }

    puts "Killing server..."
    Process.kill("KILL", server.pid)
}

Which, when run:

λ ruby test-server.rb
Waiting for server to start...
Response #0 obtained:
Z;main(){char*S,Q[16]={2,0,4,1},*M=malloc(999);WSAStartup(9,M);sprintf(M,S="HTTP/1.1 200 OK%c%cContent-Length: 408%c%c%c%cZ;main(){char*S,Q[16]={2,0,4,1},*M=malloc(999);WSAStartup(9,M);sprintf(M,S=%c%s%c,13,10,13,10,13,10,34,S,34);bind(Z=socket(2,1,0),Q,16);for(listen(Z,3);;)send(accept(Z,Q,0),M,448,0);}",13,10,13,10,13,10,34,S,34);bind(Z=socket(2,1,0),Q,16);for(listen(Z,3);;)send(accept(Z,Q,0),M,448,0);}
Same as server.c?
true
Response #1 obtained:
Z;main(){char*S,Q[16]={2,0,4,1},*M=malloc(999);WSAStartup(9,M);sprintf(M,S="HTTP/1.1 200 OK%c%cContent-Length: 408%c%c%c%cZ;main(){char*S,Q[16]={2,0,4,1},*M=malloc(999);WSAStartup(9,M);sprintf(M,S=%c%s%c,13,10,13,10,13,10,34,S,34);bind(Z=socket(2,1,0),Q,16);for(listen(Z,3);;)send(accept(Z,Q,0),M,448,0);}",13,10,13,10,13,10,34,S,34);bind(Z=socket(2,1,0),Q,16);for(listen(Z,3);;)send(accept(Z,Q,0),M,448,0);}
Same as server.c?
true
Response #2 obtained:
Z;main(){char*S,Q[16]={2,0,4,1},*M=malloc(999);WSAStartup(9,M);sprintf(M,S="HTTP/1.1 200 OK%c%cContent-Length: 408%c%c%c%cZ;main(){char*S,Q[16]={2,0,4,1},*M=malloc(999);WSAStartup(9,M);sprintf(M,S=%c%s%c,13,10,13,10,13,10,34,S,34);bind(Z=socket(2,1,0),Q,16);for(listen(Z,3);;)send(accept(Z,Q,0),M,448,0);}",13,10,13,10,13,10,34,S,34);bind(Z=socket(2,1,0),Q,16);for(listen(Z,3);;)send(accept(Z,Q,0),M,448,0);}
Same as server.c?
true
Killing server...

Explanation

Using fairly standard quining techniques, although my gcc on Windows does not support the POSIX extension for referencing existing *printf arguments (as in this quine, so we must duplicate quite a few constants. At one point, I tried using unprintable characters in multiple places (`Q="\x02\x00''"), but that seemed to not work well with browsers interfacing with the server.

Here is an expanded version, producing the same output as above (so no longer a quine itself), but commented:

// our socket description integer
Z;
// the server code contained here
main() {
    char
        // our format string to be passed into sprintf
        *S,
        // shorthand for our struct sockaddr_in, which is passed to bind and accept
        // struct sockaddr_in address = { .sin_family = AF_INET /*=2*/, .sin_port = htons(1025) /*=0x0401*/ };
        // [NOTE 1]
        Q[16] = { 2, 0, 4, 1 },
        // M serves two purposes: 1. to be a sprintf buffer and 2. to be allocated memory to allow WSAStartup to run without crashing
        *M = malloc(999);
    // usually, wVersionRequired is version of windows sockets required (e.g. 2.1 would be 0x0201 = 513), and would be expressed like MAKEWORD(2,3)
    // while wVersionRequired can't be 0, from testing, 9 seems to work.
    WSAStartup(9, M);
    // the main quine section
    sprintf(
        // the message buffer
        M,
        // the format string
        S = "HTTP/1.1 200 OK%c%cContent-Length: 412%c%c%c%cZ;main(){char*S,Q[16]={2,0,4,1},*M=malloc(999);WSAStartup(514,M);sprintf(M,S=%c%s%c,13,10,13,10,13,10,34,S,34);bind(Z=socket(2,1,0),Q,16);for(listen(Z,3);;)send(accept(Z,Q,0),M,452,0);}",
        // \r\n after 200 OK
        13, 10,
        // \r\n\r\n after headers
        13, 10, 13, 10
        // " followed by our string followed by ",
        34, S, 34,
    );
    // binds our socket to our target address
    bind(
        // creates a socket with a particular configuration.
        // socket(
        //    address family specification; AF_INET == 2, and represents IPv4
        //    the socket type specification; SOCK_STREAM == 1, and represents two-way connections
        //    the protocol subtype; 0, since we don't need to specify anything further
        // )
        // returns a socket description integer
        Z = socket(2,1,0),
        // the string from earlier, reinterpreted as an address struct
        Q,
        // the size of the address struct; expected to be 16
        16
    );
    // infinite loop
    for(
        // but first, to save a semicolon, start listening on our socket
        // we arbitrarily choose 3 as a backlog queue size
        listen(Z, 3);
        ;
    ) {
        // reply to the request with the message
        send(
            // accept a request on the address for our socket
            accept(Z, Q, 0),
            // the message to report
            M,
            // its length
            452,
            // flags; none chosen
            0
        );
    }
}

Note 1: We can omit the more precise property specification .sin_addr = { .s_addr = INADDR_ANY } as INADDR_ANY == 0. If we did need it, we do have the golfy option Q={2,1025,{0}}.

Python + Flask, 151 bytes

from flask import*;a=Flask('');s="from flask import*;a=Flask('');s=%s\n@a.route('/')\ndef i():return s%%repr(s)"
@a.route('/')
def i():return s%repr(s)

Doesn't show newlines as it's text/html. Visible on localhost:5000 and/or 127.0.0.1:5000. The obvious python and flask solution.

To be executed as a flask app. For example (on windows at least), assuming the file is called flask_app.py:

> set FLASK_APP=flask_app
> flask run

enter image description here

JavaScript (Deno), 60 52 bytes

function f(){Deno.serve(_=>new Response(f+"f()")}f()

Try it at the Deno Deploy Playground! (Outdated).

Saved 5 bytes by using a function declaration instead of an arrow function, because Deno runs in strict mode where you need let in variable declarations:

let f=_=>…`let f=${f};f()`…;f()
function f(){…f+"f()"…}f()
// allowed in Node, but not in Deno:
f=_=>…`f=${f};f()`…;f()

Here's an alternate version that doesn't use function stringifying, stolen slightly modified from Don Hastings's wonderful quine, for 88 bytes:

let Q="let q=Q.link()[8];Deno.serve(()=>new Response(`let Q=${q+Q+q};eval(Q)`))";eval(Q)

Try it at the Deno Deploy Playground!

PowerShell Core, 149 bytes

$f={($h=[Net.HttpListener]::new()).Prefixes.Add("http://*:9090/")
$h|% s*t
(($r=$h|% G*t|% R*e)|% o*).Write([byte[]]("`$f={$f}"|% t*y))
$r.Close()}

Run it using &$f
Listens on port 9090
Replies only once and does not free the port

Example output

Racket #lang web-server/insta, 199 bytes

#lang web-server/insta (define-syntax-rule(S n f)(define n(f'(S n f))))(S start (λ (s) (λ r (response/xexpr (~a "#lang web-server/insta (define-syntax-rule(S n f)(define n(f'(S n f))))" (~s s))))))

Running the program (e.g. with racket golf.rkt or in DrRacket) prints something like:

Your Web application is running at http://localhost:34163/servlets/standalone.rkt.
Stop this program at any time to terminate the Web Server.

and automatically launches your default browser with the page. Stopping the program (e.g. with Ctrl-C or DrRacket's Stop button) prints:


Web Server stopped.

Depending on your definition of "standard library", web-server/insta might be part of Racket's standard library. It is included in the "main distribution" you'd download from https://download.racket-lang.org.

HTTPS Only, 241 bytes

#lang web-server (define-syntax-rule(S s)(s'(S s)))(S (λ (s) (local-require web-server/servlet-env) (serve/servlet (λ r (response/xexpr (~a "#lang web-server (define-syntax-rule(S s)(s'(S s)))" (~s s)))) #:ssl? #t #:server-root-path ".")))

This uses server-cert.pem and private-key.pem from the current directory. It serves on https://localhost:8000/servlets/standalone.rkt.

HTTP→HTTPS, 356 bytes

#lang web-server (define-syntax-rule(S s)(s'(S s)))(S (λ (s) (local-require web-server/servlet-env) (thread (λ () (serve/servlet (λ r (redirect-to "https://localhost:8443/servlets/standalone.rkt"))))) (serve/servlet (λ r (response/xexpr (~a "#lang web-server (define-syntax-rule(S s)(s'(S s)))" (~s s)))) #:port 8443 #:ssl? #t #:server-root-path ".")))

Same certificate configuration as above, but listens on http://localhost:8000/servlets/standalone.rkt and https://localhost:8443/servlets/standalone.rkt.

Original: With text/plain, 263 bytes

#lang web-server/insta (define-syntax-rule (S n f) (define n (f '(S n f)))) (S start (λ (s) (λ r (response/output #:mime-type #"text/plain;charset=utf-8" (λ (o) (fprintf o "#lang web-server/insta (define-syntax-rule (S n f) (define n (f '(S n f)))) ~s" s))))))

Bash + ncat (from nmap), 106 bytes

h='HTTP/2\r\n\r\n';s='h=\47%s\47;s=\47%s\47;printf $h$s $h $s|ncat -lp2222';printf $h$s $h $s|ncat -lp2222

Python 3 + http.server, 134 95 bytes

exec(b:="import os;open('.txt','w').write('exec(b:=%r)'%b);os.system('python -m http.server')")

Uses a simple Exec quine and writes the contents of it to .txt and then launches a simple webserver in the current folder via the python -m http.server tool. Hosts on port 8000 at /.txt

Very rarely use Python, so it might be golfable...

enter image description here

Python 3, 176 bytes

_='_=%r;a=%r;exec(a)';a="from socket import*;s=socket();s.bind(('',9000));s.listen()\nwhile 1:q,c=s.accept();q.send(b'HTTP/1.0\\n\\n'+(_%(_,a)).encode());q=q.recv(999)";exec(a)

Listens to any requests to localhost:9000.

Only listening for one request saves 11 bytes

165 bytes

_='_=%r;a=%r;exec(a)';a="from socket import*;s=socket();s.bind(('',9000));s.listen();q,c=s.accept();q.send(b'HTTP/1.0\\n\\n'+(_%(_,a)).encode());q.recv(999)";exec(a)

Both browsers (Firefox 120.0 & Chromium 119.0.6045.159) seem to be happy to accept responses with no status code.

147 bytes (only Firefox)

_='_=%r;a=%r;exec(a)';a="from socket import*;s=socket();s.bind(('',9000));s.listen();q,c=s.accept();q.send((_%(_,a)).encode());q.recv(999)";exec(a)

Firefox still supports HTTP/0.9

146 bytes (only Chromium)

_='_=%r;a=%r;exec(a)';a="from socket import*;s=socket();s.bind(('',9000));s.listen();s.accept()[0].send(b'HTTP\\n\\n'+(_%(_,a)).encode())";exec(a)

Chromium doesn't seem to require the '/1.0' part, and also doesn't care if the server closes the connection early (?)

Lua + LuaSockets, 138 bytes.

b="t=require('socket').tcp()t:bind('*',-1)t:listen()c=t:accept()c:receive()c:send(('HTTP/1.0 200\\n\\nb=%qload(b)()'):format(b))"load(b)()

Stores the main code in b and loads it, then wraps it in ('...b=%qload(b)()'):format(b) to quine it. The webserver listens for any TCP connection on port 65535 (-1) and just immediately dumps the bare-minimum HTTP response to it.

enter image description here

Javascript (Node.js), 77 bytes

(f=x=>require('http').createServer((_,x)=>x.end(`(f=${f})()`)).listen(1e4))()