2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -9,10 +9,16 @@
|
|||
|
||||
The browser is the new [[GUI]] !
|
||||
|
||||
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>
|
||||
|
||||
;Task:
|
||||
Serve our standard text <big><big><code>Goodbye, World!</code></big></big> 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.
|
||||
|
||||
Note that starting a web browser or opening a new window with this URL
|
||||
is not part of the task. <br>
|
||||
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.
|
||||
<br><br>
|
||||
|
|
|
|||
14
Task/Hello-world-Web-server/Fortran/hello-world-web-server.f
Normal file
14
Task/Hello-world-Web-server/Fortran/hello-world-web-server.f
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
program http_example
|
||||
implicit none
|
||||
character (len=:), allocatable :: code
|
||||
character (len=:), allocatable :: command
|
||||
logical :: waitForProcess
|
||||
|
||||
! Execute a Node.js code
|
||||
code = "const http = require('http'); http.createServer((req, res) => &
|
||||
{res.end('Hello World from a Node.js server started from Fortran!')}).listen(8080);"
|
||||
|
||||
command = 'node -e "' // code // '"'
|
||||
call execute_command_line (command, wait=waitForProcess)
|
||||
|
||||
end program http_example
|
||||
|
|
@ -1,8 +1,8 @@
|
|||
def app(environ, start_response):
|
||||
start_response('200 OK', [])
|
||||
yield "Goodbye, World!"
|
||||
from wsgiref.simple_server import make_server
|
||||
|
||||
if __name__ == '__main__':
|
||||
from wsgiref.simple_server import make_server
|
||||
server = make_server('127.0.0.1', 8080, app)
|
||||
server.serve_forever()
|
||||
def app(environ, start_response):
|
||||
start_response('200 OK', [('Content-Type','text/html')])
|
||||
yield b"<h1>Goodbye, World!</h1>"
|
||||
|
||||
server = make_server('127.0.0.1', 8080, app)
|
||||
server.serve_forever()
|
||||
|
|
|
|||
|
|
@ -1,2 +1,4 @@
|
|||
require 'sinatra'
|
||||
get("/") { "Goodbye, World!" }
|
||||
require 'webrick'
|
||||
WEBrick::HTTPServer.new(:Port => 80).tap {|srv|
|
||||
srv.mount_proc('/') {|request, response| response.body = "Goodbye, World!"}
|
||||
}.start
|
||||
|
|
|
|||
|
|
@ -0,0 +1,2 @@
|
|||
require 'sinatra'
|
||||
get("/") { "Goodbye, World!" }
|
||||
|
|
@ -1,28 +1,27 @@
|
|||
use std::io::net::tcp::{TcpListener, TcpStream};
|
||||
use std::io::net::ip::{Ipv4Addr, SocketAddr};
|
||||
use std::io::{Acceptor, Listener};
|
||||
use std::net::{Shutdown, TcpListener};
|
||||
use std::thread;
|
||||
use std::io::Write;
|
||||
|
||||
const RESPONSE: &'static [u8] = b"HTTP/1.1 200 OK\r
|
||||
Content-Type: text/html; charset=UTF-8\r\n\r
|
||||
<!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";
|
||||
|
||||
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 listener = TcpListener::bind("127.0.0.1:8080").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());
|
||||
})
|
||||
for stream in listener.incoming() {
|
||||
thread::spawn(move || {
|
||||
let mut stream = stream.unwrap();
|
||||
match stream.write(RESPONSE) {
|
||||
Ok(_) => println!("Response sent!"),
|
||||
Err(e) => println!("Failed sending response: {}!", e),
|
||||
}
|
||||
stream.shutdown(Shutdown::Write).unwrap();
|
||||
});
|
||||
}
|
||||
|
||||
drop(acceptor);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue