Hello world/Web server: Difference between revisions

Content added Content deleted
(→‎{{header|Common Lisp}}: proper indentation for serve function)
m (-Category:Scala Implementations / moved Category to top)
Line 1: Line 1:
{{task|Networking and Web Interaction}}
{{task|Networking and Web Interaction}} [[Category:Web]]
{{omit from|GUISS}}
The browser is the new [[GUI]]!
{{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 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. The provided solution must start or implement a server that accepts multiple client connections and serves text as requested.
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. 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.
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.


=={{header|Ada}}==
=={{header|Ada}}==
Line 32: Line 45:


=={{header|AWK}}==
=={{header|AWK}}==
With GNU AWK (gawk) a simple web server can be implemented. The example is taken from here
With GNU AWK (gawk) a simple web server can be implemented. <br>
The example is taken from
[http://www.gnu.org/software/gawk/manual/gawkinet/gawkinet.html#Primitive-Service]
[http://www.gnu.org/software/gawk/manual/gawkinet/gawkinet.html#Primitive-Service]
(Documentation is licensed under GNU Free Documentation License, Version 1.3)
(Documentation is licensed under GNU Free Documentation License, Version 1.3)
Line 284: Line 298:


=={{header|D}}==
=={{header|D}}==
Using sockets only, also shows use of heredoc syntax, std.array.replace, and casting to bool to satisfy the while conditional.
Using sockets only, also shows use of heredoc syntax, std.array.replace,
and casting to bool to satisfy the while conditional.


<lang D>
<lang D>
Line 323: Line 338:
..response.close()));
..response.close()));
}</lang>
}</lang>

=={{header|Delphi}}==
=={{header|Delphi}}==
<lang Delphi>program HelloWorldWebServer;
<lang Delphi>program HelloWorldWebServer;
Line 416: Line 432:


=={{header|Erlang}}==
=={{header|Erlang}}==
Using builtin HTTP server with call back to do/1. It only lasts 30 seconds (30000 milliseconds), then it is stopped. I fail to see how a longer time will serve any purpose.
Using builtin HTTP server with call back to do/1.
It only lasts 30 seconds (30000 milliseconds), then it is stopped.
I fail to see how a longer time will serve any purpose.


<lang Erlang>
<lang Erlang>
Line 503: Line 521:
=={{header|Haskell}}==
=={{header|Haskell}}==


Lightweightly concurrent "hello world" web server using the [http://www.yesodweb.com/book/conduits conduit] stack:
Lightweightly concurrent "hello world" web server
using the [http://www.yesodweb.com/book/conduits conduit] stack:


<lang haskell>{-# LANGUAGE OverloadedStrings #-}
<lang haskell>{-# LANGUAGE OverloadedStrings #-}
Line 575: Line 594:


=={{header|J}}==
=={{header|J}}==
If the desire is to use the browser as a gui, the easiest thing to do would be to [http://www.jsoftware.com/stable.htm download] [http://www.jsoftware.com/docs/help701/user/relhigh.htm j7], edit the jhs script to start on port 8080, start jhs, visit http://127.0.0.1:8080/jijx then enter the text:
If the desire is to use the browser as a gui, the easiest thing to do
would be to [http://www.jsoftware.com/stable.htm download] [http://www.jsoftware.com/docs/help701/user/relhigh.htm j7], edit the jhs script to start on port 8080,
start jhs, visit http://127.0.0.1:8080/jijx then enter the text:
<lang j>'Goodbye, World!'</lang>
<lang j>'Goodbye, World!'</lang>
This will compute the desired result and display it (actually, it will be displayed twice since the original string will also be displayed). This would be even simpler if you could just use the default jhs port (65001)... Alternatively, a jhs form could be used (but this would not have the exact url structure specified).
This will compute the desired result and display it (actually, it will be displayed twice since the original string will also be displayed).
This would be even simpler if you could just use the default jhs port (65001)...
Alternatively, a jhs form could be used (but this would not have the exact url structure specified).


However, if the desire is to implement the task exactly, any of approaches at [[j:JWebServer]] might be used.
However, if the desire is to implement the task exactly,
any of approaches at [[j:JWebServer]] might be used.


For example, here is a web server which ignores the client's request and always returns Goodbye, World:
For example, here is a web server which ignores the client's request
and always returns Goodbye, World:
<lang j>hello=: verb define
<lang j>hello=: verb define
8080 hello y NB. try to use port 8080 by default
8080 hello y NB. try to use port 8080 by default
Line 611: Line 636:
To deploy this server, once it has been defined, run
To deploy this server, once it has been defined, run
<lang j>hello''</lang>
<lang j>hello''</lang>
This version works because reasonable http requests fit in a single tcp packet.
This version works because reasonable http requests fit in a single tcp packet. (And note that the server waits for one tcp packet before responding.) If parsing of the request is desired, one of the more complicated implementations at [[j:JWebServer]] should be used instead (but that's not really relevant for this task, except perhaps to require complete headers before responding, with broken browsers which send multiple tcp packets for the request).
(And note that the server waits for one tcp packet before responding.)
If parsing of the request is desired, one of the more complicated implementations at [[j:JWebServer]] should be used instead (but that's not really relevant for this task, except perhaps to require complete headers before responding, with broken browsers which send multiple tcp packets for the request).


=={{header|Java}}==
=={{header|Java}}==
Multiple requests will be served in the order that they reach the server, with a queue size limit of 50 waiting requests imposed by default in the <code>ServerSocket</code> class (may be changed by adding a second positive integer argument to the <code>ServerSocket</code> constructor).
Multiple requests will be served in the order that they reach the server,
with a queue size limit of 50 waiting requests imposed by default in the <code>ServerSocket</code> class (may be changed by adding a second positive integer argument to the <code>ServerSocket</code> constructor).
<lang java>import java.io.IOException;
<lang java>import java.io.IOException;
import java.io.PrintWriter;
import java.io.PrintWriter;
Line 657: Line 685:


=={{header|Lasso}}==
=={{header|Lasso}}==
While Lasso has a built-in webserver you can use, here's how you can create a basic multi-threaded webserver of your own to complete this request:
While Lasso has a built-in webserver you can use,
here's how you can create a basic multi-threaded webserver
of your own to complete this request:
<lang lasso>local(server) = net_tcp
<lang lasso>local(server) = net_tcp
handle => { #server->close }
handle => { #server->close }
Line 680: Line 710:


=={{header|Liberty BASIC}}==
=={{header|Liberty BASIC}}==
This is difficult, although possible, in Liberty BASIC, but it's close relative Run BASIC is designed for serving webpages easily. The task becomes simply ..
This is difficult, although possible, in Liberty BASIC,
but it's close relative ''Run BASIC'' is designed for serving webpages easily.
The task becomes simply ..
<lang lb>print "hello world!" </lang>
<lang lb>print "hello world!" </lang>


Line 863: Line 895:
close CLIENT;
close CLIENT;
}</lang>
}</lang>
Various modules exist for using sockets, including the popular IO::Socket which provides a simpler and more friendly OO interface for the socket layer. Here is the solution using this module:
Various modules exist for using sockets, including the popular IO::Socket
which provides a simpler and more friendly OO interface for the socket layer.
Here is the solution using this module:
{{libheader|IO::Socket::INET}}
{{libheader|IO::Socket::INET}}
<lang Perl>use IO::Socket::INET;
<lang Perl>use IO::Socket::INET;
Line 877: Line 911:
close $client;
close $client;
}</lang>
}</lang>

Using Perl's glue power, provide a suicide note with visitor counter via netcat:
Using Perl's glue power, provide a suicide note
with visitor counter via netcat:
<lang Perl>while (++(our $vn)) {
<lang Perl>while (++(our $vn)) {
open NC, "|-", qw(nc -l -p 8080 -q 1);
open NC, "|-", qw(nc -l -p 8080 -q 1);
Line 884: Line 920:
"Goodbye, World! (hello, visitor No. $vn!)\xd\xa";
"Goodbye, World! (hello, visitor No. $vn!)\xd\xa";
}</lang>
}</lang>

Here's another solution using Plack (may be found on CPAN):
Here's another solution using Plack (may be found on CPAN):
<lang Perl>use Plack::Runner;
<lang Perl>use Plack::Runner;
Line 1,055: Line 1,092:
=={{header|Run BASIC}}==
=={{header|Run BASIC}}==
<lang runbasic>html "Hello World!"</lang>
<lang runbasic>html "Hello World!"</lang>

=={{header|Rust}}==
=={{header|Rust}}==
This solution works for Rust 0.11-pre.
This solution works for Rust 0.11-pre.
Line 1,087: Line 1,125:


=={{header|Scala}}==
=={{header|Scala}}==
[[Category:Scala Implementations]]
{{libheader|Scala}}
{{libheader|Scala}}
{{Trans|Java}}It shows that Scala can simply embed XML fragments.
{{Trans|Java}}It shows that Scala can simply embed XML fragments.
Line 1,164: Line 1,201:
service registerServiceOn: myServer.
service registerServiceOn: myServer.
myServer start.</lang>
myServer start.</lang>
Be aware that the above is an ad-hoc minimal scripting example. Normally, a service subclass is used and response handlers are defined as methods of it (not as action blocks).
Be aware that the above is an ad-hoc minimal scripting example.
Normally, a service subclass is used and
Also, services and HTML generation is usually done using a framework (at least DOM-based, but usually a higher level toolkit).
response handlers are defined as methods of it (not as action blocks).
Also, services and HTML generation is usually done using a framework
(at least DOM-based, but usually a higher level toolkit).
Especially take a look at smalltalk frameworks like Aida, Seaside, VisualWave etc.
Especially take a look at smalltalk frameworks like Aida, Seaside, VisualWave etc.

===Pharo Smalltalk===
===Pharo Smalltalk===
Pharo ships with the Zinc HTTP Component frameworks that includes a ZnServer class. Here's the simplest solution to start a web server:
Pharo ships with the Zinc HTTP Component frameworks
that includes a ZnServer class.
Here's the simplest solution to start a web server:
<lang smalltalk>
<lang smalltalk>
(ZnServer startDefaultOn: 1701)
(ZnServer startDefaultOn: 1701)
Line 1,191: Line 1,234:
socket -server accept 8080
socket -server accept 8080
vwait forever</lang>
vwait forever</lang>

===Jim Tcl===
===Jim Tcl===
Jim is a small footprint reimplementation of Tcl with modern features.
Jim is a small footprint reimplementation of Tcl with modern features.
Line 1,265: Line 1,309:
serverSocket.hostname, ":", serverSocket.port);
serverSocket.hostname, ":", serverSocket.port);
serverSocket.listen(jobPipe);</lang>
serverSocket.listen(jobPipe);</lang>


{{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}}