Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -1,5 +1,18 @@
The browser is the new [[GUI]]!
{{omit from|GUISS}}
{{omit from|Locomotive Basic|No sockets}}
{{omit from|Lotus 123 Macro Scripting}}
{{omit from|Maxima}}
{{omit from|ML/I|No sockets}}
{{omit from|Retro}}
{{omit from|TI-83 BASIC}}
{{omit from|ZX Spectrum Basic|No sockets}}
The task is to serve our standard text "Goodbye, World!" to http://localhost:8080/ so that it can be viewed with a web browser. The provided solution must start or implement a server that accepts multiple client connections and serves text as requested.
The browser is the new [[GUI]] !
Note that starting a web browser or opening a new window with this URL is not part of the task. Additionally, it is permissible to serve the provided page as a plain text file (there is no requirement to serve properly formatted [[HTML]] here). The browser will generally do the right thing with simple text like this.
The task is to serve our standard text "Goodbye, World!" to http://localhost:8080/ so that it can be viewed with a web browser. <br>
The provided solution must start or implement a server that accepts multiple client connections and serves text as requested.
Note that starting a web browser or opening a new window with this URL
is not part of the task. <br>
Additionally, it is permissible to serve the provided page as a plain text file (there is no requirement to serve properly formatted [[HTML]] here).
The browser will generally do the right thing with simple text like this.

View file

@ -1,2 +1,4 @@
---
category:
- Web
note: Networking and Web Interaction

View file

@ -10,7 +10,7 @@
char response[] = "HTTP/1.1 200 OK\r\n"
"Content-Type: text/html; charset=UTF-8\r\n\r\n"
"<doctype !html><html><head><title>Bye-bye baby bye-bye</title>"
"<!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 red}</style></head>"

View file

@ -0,0 +1,12 @@
(ns goodbye-world.handler
(:require [compojure.core :refer :all]
[compojure.handler :as handler]
[compojure.route :as route]))
(defroutes app-routes
(GET "/" [] "Goodbye, World!")
(route/resources "/")
(route/not-found "Not Found"))
(def app
(handler/site app-routes))

View file

@ -19,19 +19,20 @@
(defun serve (port &optional (log-stream *standard-output*))
(let ((connections (list (socket-listen "127.0.0.1" port :reuse-address t))))
(unwind-protect
(loop (loop for ready in (wait-for-input connections :ready-only t)
do (if (typep ready 'stream-server-usocket)
(push (socket-accept ready) connections)
(let* ((stream (socket-stream ready)))
(read-all stream)
(format log-stream "Got message...~%")
(mapc (lambda (line) (ln line stream))
(list "HTTP/1.1 200 OK"
"Content-Type: text/plain; charset=UTF-8"
""
"Hello world!"))
(socket-close ready)
(setf connections (remove ready connections))))))
(loop
(loop for ready in (wait-for-input connections :ready-only t)
do (if (typep ready 'stream-server-usocket)
(push (socket-accept ready) connections)
(let* ((stream (socket-stream ready)))
(read-all stream)
(format log-stream "Got message...~%")
(mapc (lambda (line) (ln line stream))
(list "HTTP/1.1 200 OK"
"Content-Type: text/plain; charset=UTF-8"
""
"Hello world!"))
(socket-close ready)
(setf connections (remove ready connections))))))
(loop for c in connections do (loop while (socket-close c))))))
(serve 8080)

View file

@ -0,0 +1,28 @@
use std::io::net::tcp::{TcpListener, TcpStream};
use std::io::net::ip::{Ipv4Addr, SocketAddr};
use std::io::{Acceptor, Listener};
fn handle_client(mut stream: TcpStream) {
let response = bytes!("HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=UTF-8\r\n\r\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 red}</style></head><body><h1>Goodbye, world!</h1></body></html>\r\n");
match stream.write(response) {
Ok(()) => println!("Response send!"),
Err(e) => println!("Failed sending response: {}!", e),
}
drop(stream);
}
fn main() {
let addr = SocketAddr { ip: Ipv4Addr(127, 0, 0, 1), port: 8080 };
let listener = TcpListener::bind(addr).unwrap();
let mut acceptor = listener.listen();
println!("Listening for connections on port {}", addr.port);
for stream in acceptor.incoming() {
spawn(proc() {
handle_client(stream.unwrap());
})
}
drop(acceptor);
}

View file

@ -0,0 +1,3 @@
(ZnServer startDefaultOn: 1701)
onRequestRespond: [ :request |
ZnResponse ok: (ZnEntity text: 'Hello World!') ].

View file

@ -0,0 +1 @@
ZnServer stopDefault.

View file

@ -0,0 +1,8 @@
set s [socket stream.server 127.0.0.1:8080]
$s readable {
set client [$s accept]
$client puts "HTTP/1.1 200 OK\nConnection: close\nContent-Type: text/plain\n"
$client puts "Hello, World!\n"
$client close
}
vwait done

View file

@ -0,0 +1 @@
while true; do { echo -e 'HTTP/1.1 200 OK\r\n'; echo 'Hello, World!'; } | nc -l 8080; done