September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -0,0 +1,34 @@
void
serve(dispatch w, file s, list colors)
{
file i, o;
date d;
accept(i, o, s, 0);
f_(o, "HTTP/1.1 200 OK\n"
"Content-Type: text/html; charset=UTF-8\n\n"
"<!DOCTYPE html><html><head><title>Bye-bye baby bye-bye</title>"
"<style>body { background-color: #111 }"
"h1 { font-size:4cm; text-align: center; color: black;"
" text-shadow: 0 0 2mm ", colors[drand(3)], "}</style></head>"
"<body><h1>Goodbye, world!</h1></body></html>\n");
# chrome won't show the page if we close right away. we'll close in 2s.
d_now(d);
d_offset(d, 2, 0);
w_schedule(w, d, f_close, i);
}
integer
main(void)
{
dispatch w;
file s;
tcpip_listen(s, 8080, 0);
w_watch(w, s, serve, w, s, l_effect("blue", "green", "red", "yellow"));
w_press(w);
return 0;
}

View file

@ -0,0 +1,11 @@
file i, o, s;
tcpip_listen(s, 8080, 0);
while (1) {
accept(i, o, s, 0);
f_(o, "HTTP/1.1 200 OK\n"
"Content-Type: text/html; charset=UTF-8\n\n"
"<!DOCTYPE html><html><head><title>baby bye-bye</title></head>"
"<body><h1>Goodbye, world!</h1></body></html>\n");
f_flush(o);
}

View file

@ -0,0 +1,47 @@
' Define HTTP constants
CONST New$ = CHR$(13) & NL$
CONST Sep$ = CHR$(13) & NL$ & CHR$(13) & NL$
CONST Msg$ = "<html><head>BaCon web greeting</head><body><h2>Goodbye, World!</h2></body></html>"
' Get our IP
Ip$ = "localhost"
PRINT "Connect from browser '", Ip$, ":8080'."
' Ignore child signals to avoid zombie processes
SIGNAL SIG_IGN, SIGCHLD
' Keep receiving requests
WHILE TRUE
' Open listening port
OPEN Ip$ & ":8080" FOR SERVER AS mynet
' Incoming connection -> create background process
spawn = FORK
' We are in the child
IF spawn = 0 THEN
' Get the request
REPEAT
RECEIVE dat$ FROM mynet
PRINT dat$
UNTIL RIGHT$(dat$, 4) = Sep$
' Reply that we're OK
SEND "HTTP/1.1 200 Ok" & New$ & "Content-Length: " & STR$(LEN(Msg$)) & Sep$ & Msg$ TO mynet
' Close connection
CLOSE SERVER mynet
' End this process
END
' We are in the parent
ELIF spawn > 0 THEN
' Close connection in parent
CLOSE SERVER mynet
ENDIF
WEND

View file

@ -1,9 +1,11 @@
import 'dart:io';
void main(){
HttpServer.bind('localhost', 8080)
.then((HttpServer server) =>
server.listen((HttpRequest request) =>
request..response.write('Hello, world')
..response.close()));
main() async {
var server = await HttpServer.bind('127.0.0.1', 8080);
await for (HttpRequest request in server) {
request.response
..write('Hello, world')
..close();
}
}

View file

@ -0,0 +1,13 @@
#javaj#
<frames> oConsole
#listix#
<main>
MICOHTTP, START, myServer, 8080
<GET />
//<html><body>
// Goodbye world!
//</body></html>

View file

@ -0,0 +1,5 @@
using HttpServer
server = Server() do req, res
"Goodbye, World!"
end
run(server, 8080)

View file

@ -0,0 +1,11 @@
import java.io.PrintWriter
import java.net.ServerSocket
fun main(args: Array<String>) {
val listener = ServerSocket(8080)
while(true) {
val sock = listener.accept()
PrintWriter(sock.outputStream, true).println("Goodbye, World!")
sock.close()
}
}

View file

@ -0,0 +1,14 @@
(import (lib http))
(http:run 8080 (lambda (fd request headers send close)
(send "HTTP/1.0 200 OK\n"
"Connection: close\n"
"Content-Type: text/html; charset=UTF-8\n"
"Server: " (car *version*) "/" (cdr *version*)
"\n\n"
"<h1>Goodbye, World!</h1>"
(ref request 1) ": " (ref request 2)
"<hr><small>" headers "</small>")
(close #t)
))

View file

@ -0,0 +1,12 @@
/* HTTP hello server */
response.1 = 'HTTP/1.1 200 OK' || '0D0A'X,
|| 'Connection: close' || '0D0A'X,
|| 'Content-Type: text/html' || '0D0A0D0A'X
response.2 = '<!DOCTYPE html>' || '0A'X,
|| '<html><head><title>Hello, Rosetta</title></head>' || '0A'X,
|| '<body><h2>Goodbye, World!</h2></body>' || '0A'X,
|| '<!-- Shout out from the Rosetta Code programming chrestomathy --></html>' || '0A'X
DO FOREVER
ADDRESS SYSTEM 'nc -l 8080' WITH INPUT STEM response.
END

View file

@ -0,0 +1,46 @@
const PORT=8080;
const SERVLET_THREADS=4;
// A class to process requests from clients (eg browsers)
// in a thread. Requests are received via a pipe, which feeds
// all Servlet threads.
class Servlet{
fcn init(jobPipe){ self.launch(jobPipe); }
fcn liftoff(jobPipe){
while(1){ // read request, write response, repeat
socket:=jobPipe.read();
if(socket.wait(60) != 1) // what is Chrome doing?
{ socket.close(); continue; }
if (request:=socket.read())
try{ processRequest(request,socket); } catch{}
}
}
fcn splashdown(h,e){ println("Servlet died before its time"); }
}
fcn processRequest(request,socket){
response:=responseHeader();
response+="Goodbye, World!";
socket.write(response); socket.close(); // no Keep-Alive
}
fcn responseHeader(status=200,reason="OK"){
String(
"HTTP/1.0 ",status," ",reason,"\r\n",
Time.Date.httpDate(),"\r\n"
"Server: ZTWS (zkl)\r\n"
"Connection: close\r\n"
"Content-Type: text/html; charset=UTF-8\r\n"
"\r\n")
}
//////////////////// Start the server ///////////////////////
var jobPipe=Thread.Pipe(); // a queue of requests
do(SERVLET_THREADS){ Servlet(jobPipe) } // start threads
// Create the HTTP server listen socket
// Sits here forever passing client HTTP connects to Servlets
serverSocket:=Network.TCPServerSocket.open(PORT);
println("HTTP server started at http://",
serverSocket.hostname, ":", serverSocket.port);
serverSocket.listen(jobPipe);