418: I'm a teapot

68

19

As we all should know, there's a HTTP status code 418: I'm a teapot.

Your mission, should you choose to accept it, is to use your creativitea and write the smallest possible server that responds with the above status code to any and every HTTP request made to it.

Standard loopholes apply, including

Fetching the desired output from an external source

This includes doing an HTTP request to fetch the page with the question and extracting a solution from that page. This was mildly amusing back in 2011, but now is derivative and uninteresting.

Meaning you cannot simply redirect the request to another server to have it return the response.


Addressing some confusion about server functionality:
Your server can do anything (or nothing) while no HTTP request is made to it as long as it replies with the correct response once a HTTP request is made.

Nit

Posted 2014-11-19T12:42:16.453

Reputation: 2 667

@Luminous It was created for Internet Teapots, and those now exist, so... – wizzwizz4 – 2016-03-20T07:29:27.937

@wizzwizz4 HTeaTeaPot – mbomb007 – 2017-02-28T20:57:18.060

2@mbomb007 Hyper Tea Transfer Pot. – wizzwizz4 – 2017-02-28T22:19:20.977

1

How much use can we make of external libraries? (e.g. using express for a node.js server)

– Martin Ender – 2014-11-19T13:02:04.503

1Should we take the “HTTP” in “any and every HTTP request” literally? I mean, if the request is not HTTP request (or even no request at all, so the client just connects and says nothing), should the server still reply or not? – manatwork – 2014-11-19T13:02:28.667

Is it strictly HTTP when the server immediately sends a response without waiting for the request? (As most of the answers here are doing.) – billpg – 2014-11-19T15:25:47.883

15to each and every http request? Surely just ones to a coffee: URI? – dave – 2014-11-19T18:24:16.570

3May we assume we already have root privileges? (e.g. bind() to port 80 is ok) – Digital Trauma – 2014-11-19T20:03:39.970

1@MartinBüttner All libraries are welcome, as many languages have such functionality built-in. – Nit – 2014-11-19T23:26:28.943

2@DigitalTrauma Yes, you can assume escalated privileges for your server. – Nit – 2014-11-19T23:26:50.730

1@manatwork Responding to non-HTTP requests is undefined behavior, meaning your program can do anything (or nothing). As long as it replies with the correct response once a HTTP request is made. – Nit – 2014-11-19T23:40:00.933

@billpg See the comment above. – Nit – 2014-11-19T23:49:35.283

1Most of the answers below do not output the required newlines to signal the end of HTTP response; most browsers would just treat them as prematurely terminated connection. – Lie Ryan – 2014-11-20T00:39:17.597

@LieRyan Feel free to point out the error to the related answers. – Nit – 2014-11-20T01:11:38.327

Would be something like a config line in httpd.conf ok? – Knerd – 2014-11-20T15:39:53.430

4@Knerd I'm leaning on the no side, you're not writing a program, you're simply configuring one. – Nit – 2014-11-20T17:10:15.487

2omg. With the IOT coming around, this status code may have an actual reason to exist! – Luminous – 2014-11-20T18:48:33.290

2creativitea? iron druid? :D – kukac67 – 2014-11-20T19:13:18.817

@Pietu1998 https://gist.github.com/red-green/474a370f7c3273403827 mine is better

– TheDoctor – 2014-11-26T01:51:59.827

Answers

39

GNU Awk: 69 characters

A server itself (endlessly serves one request at a time), no library used.

Send 418 to everybody who connects (82 69 characters):

BEGIN{while(s="/inet/tcp/80/0/0"){print"HTTP/1.1 418\n"|&s
close(s)}}

Send 418 to everybody who sends something (93 80 characters):

BEGIN{while(s="/inet/tcp/80/0/0"){s|&getline
print"HTTP/1.1 418\n"|&s
close(s)}}

Send 418 to everybody who sends a valid HTTP GET request (122 109 characters):

BEGIN{while(s="/inet/tcp/80/0/0"){s|&getline
if(/^GET \S+ HTTP\/1\.[01]$/)print"HTTP/1.1 418\n"|&s
close(s)}}

manatwork

Posted 2014-11-19T12:42:16.453

Reputation: 17 865

1You could possibly save a byte by using smaller port like 8, which is unassigned. – Hannes Karppila – 2016-03-20T09:01:15.607

@HannesKarppila, that's correct. But given that all other solutions which explicitly specify a port (excepting the Haskell answer's port 8888) uses port 80, I better keep it like this for comparability. – manatwork – 2016-03-21T08:30:31.683

But .. how to connect ? ;) – Optimizer – 2014-11-19T13:31:05.770

5

The same way you connect to any web server. Browser, netcat, telnet, wget, curl, another GNU awk script, … Theoretically it listens on localhost:80, but for that 1) no web server, Skype or other program should use port 80; 2) you have to be super user to open ports below 1024. So for testing is easier to edit the port number in the script to something like 8080 (s="/inet/tcp/8080/0/0") then connect to that. http://pastebin.com/zauP7LMA

– manatwork – 2014-11-19T13:43:23.657

2I see. Cool. Awk noob here :) – Optimizer – 2014-11-19T14:10:39.460

32

Bash (33)

nc -lp80 -q1<<<"HTTP/1.1 418
";$0

marinus

Posted 2014-11-19T12:42:16.453

Reputation: 30 224

3Are the two newlines necessary? Seems to work OK with only one for me. Also by my count the above is 34 bytes - I'm guessing your editor is adding an unnecessary newline or \0 to the end of your file - you can truncate this and it still works. s='echo HTTP/1.1 418|nc -lp80 -q1;$0' ; echo ${#s} gives 33 for me – Digital Trauma – 2014-11-20T00:56:38.427

@DigitalTrauma: You are right - the final newline is added automatically in here strings: Why does a bash here-string add a trailing newline char? ------ But I see two possible problems: 1. The script expects that it is stored in one of the $PATH directories or that it is called with a path (recursion by $0). ----- 2. HTTP requires that lines are terminated by \r\n not only \n. Here string should be $"HTTP/1.1 418\r\n\r" (longer readable form). ------ And finally: The script could be shorter: The -q1 parameter is not necessary.

– pabouk – 2014-11-20T09:02:32.397

2@DigitalTrauma, @pabouk: The output needs end with two newlines. So one of the newlines was unnecessary (because of the here-string), but the echo variant does not work (at least Firefox won't recognize it as a 418). However, the \rs aren't needed. The spec says it should be \r\n\r\n, but at least Firefox and Chrome will accept \n\n, so it seems to be in the spirit of golfing not to include them. The -q1 parameter was necessary on my system, because the browser will not close the connection by itself. $0 works fine if the script is made executable and called in that manner. – marinus – 2014-11-20T15:28:31.493

@marinus Interesting - I was testing with wget which seems to be fine with just one newline – Digital Trauma – 2014-11-20T16:06:58.647

1

@pabouk - Interesting snippet here http://www.w3.org/Protocols/HTTP/OldServers.html: "Lines should be regarded as terminated by the Line Feed, and the preceeding Carriage Return character ignored"

– Digital Trauma – 2014-11-20T16:09:19.477

Regarding -q1: I think that the problem you observed could be related to persistent connections officially defined in HTTP/1.1. I think you should try to use HTTP/1.0 instead. This could work without -q1. ------ Regarding $0 recursion: as the script does not have the shebang sequence and requires root privileges, I called it sudo bash scriptname. This does not work if the directory is not in the $PATH. ------ I did a mistake in my previous comment. The bash code for CR is $'\r' not $"\r".

– pabouk – 2014-11-20T20:32:59.450

@pabouk: making this executable and calling it sudo ./scriptname.sh works perfectly fine over here. As for the -q1, Firefox doesn't close the connection even with HTTP/1.0, so nc doesn't end and any further requests will fail. – marinus – 2014-11-20T21:48:28.697

30

PHP - 85 bytes

<?for($s=socket_create_listen(80);socket_write(socket_accept($s),"HTTP/1.1 418
"););

Saved with Windows-Style (CR-LF) line endings, requires php_sockets enabled.

I actually used this as my error code for the Hard Code Golf: Create a Chatroom challenge, but no one noticed.


Browser-Friendly version

<?for(socket_getsockname($s=socket_create_listen(80),$n);$t="I'm a teapot";socket_write($c=socket_accept($s),"HTTP/1.0 418 $t
Content-Length: $l

<title>418 $t</title><h1>$t</h1>The requested resource is not suitable for brewing coffee.<hr><i>$n:80</i>"))$l=124+strlen($n);

Start the script in the CLI, and point your browser at http://localhost.

primo

Posted 2014-11-19T12:42:16.453

Reputation: 30 891

1+1 for user firendlyness / I'm a teapot :-) – Levite – 2015-02-25T07:28:52.657

20

Node.js (LiveScript)

http module - 66

require(\http)createServer (->&1.writeHead 418;&1.end!) .listen 80

Inspired by Qwertiy's answer.

net module - 76

require(\net)createServer (->it.write 'HTTP/1.1 418\r\n';it.end!) .listen 80

nyuszika7h

Posted 2014-11-19T12:42:16.453

Reputation: 1 624

2Could the person who downvoted care to explain why? – nyuszika7h – 2014-11-30T17:40:43.157

20

Ruby + Rack, 19 bytes

run->e{[418,{},[]]}

Must be saved as config.ru, and run with the rackup command.

Or if you prefer "pure" Ruby:

Rack::Handler::WEBrick.run->e{[418,{},[]]}

42 bytes + -rrack flag = 48 bytes

August

Posted 2014-11-19T12:42:16.453

Reputation: 706

12

Bash+BSD general commands, 29

Borrowing back a little bit from other answers:

nc -lp80<<<"HTTP/1.1 418
";$0

Works for me with wget.

First answer to use nc, 38

for((;;)){
nc -l 80 <<<HTTP/1.1\ 418
}

I'm assuming root privileges - run as follows:

sudo bash ./418.sh

Digital Trauma

Posted 2014-11-19T12:42:16.453

Reputation: 64 644

This also assumes that nc is installed

– ub3rst4r – 2014-11-20T00:33:34.187

3@ub3rst4r Correct - That is why I stated "BSD general commands", which may be considered as a "library" from the point of view of shell scripting. From the OP: "All libraries are welcome" – Digital Trauma – 2014-11-20T00:37:25.403

2A response must end with a newline, see w3.org/Protocols/HTTP/Response.html – Nit – 2014-11-20T06:57:18.013

2@Nit - Yes - bash "here strings" will automatically be appended with a newline – Digital Trauma – 2014-11-20T16:03:47.350

1How about nc -l 80 <<<HTTP/1.1\ 418;$0 – core1024 – 2014-11-20T16:46:04.190

The spaces around 80 are not necessary but there is a different problem: HTTP headers section should be terminated by two newlines. – pabouk – 2014-11-20T17:48:49.243

@pabouk - My version of nc seems to expect space between -l and 80; -lp80 can also be used, though doesn't save anything. My expectation was the same about the space after 80, but my bash (4.3.11) doesn't like it. Try fold -w 1<<<abc vs fold -w 1 <<<abc - the 1<<<abc is parsed first as a redirection before the rest of the command is expanded. – Digital Trauma – 2014-11-20T18:25:59.290

@pabouk As for the two newlines, I think this is necessary when content follows, but I think any sensible user agent should be able to handle error header followed by end of connection with just one newline. At least wget, curl and chrome Postman REST client seem to be OK. I couldn't really tell what firefox was doing. – Digital Trauma – 2014-11-20T18:27:01.110

@pabouk Ok, in the interests of compliance, I've put in 2 newlines. I was able to save back one byte by using -lp80<<< which seems to help the previous bash redirect problem – Digital Trauma – 2014-11-20T18:44:36.403

1Sorry for the confusion with the two spaces. I did not notice that you did not use the -p switch :) I tested the code with Firefox and without two newlines the status code is not recognized. – pabouk – 2014-11-20T20:21:53.433

9

Ruby (nc system command) - 35

loop{`nc -l 80 <<<"HTTP/1.1 418"`}

DigitalTrauma should get the credit for the idea of using nc, however Ruby can make an infinite loop with fewer characters than Bash :)

Ruby (TCPServer) - 75

require'socket'
s=TCPServer.new 80
loop{(s.accept<<'HTTP/1.1 418
').close}

That newline is intentional -- the actual newline character is one character shorter than "\n".

Ruby (WEBrick HTTPServer) - 87

require'webrick'
(s=WEBrick::HTTPServer.new).mount_proc(?/){|_,r|r.status=418}
s.start

Trey Thomas

Posted 2014-11-19T12:42:16.453

Reputation: 296

A response must end with a newline, see http://www.w3.org/Protocols/HTTP/Response.html

– Nit – 2014-11-20T06:56:57.860

1@DigitalTrauma, I was going to use that, but then realized the backslash needs to be escaped with another backslash, so it would have been the same number of characters anyway :) – Trey Thomas – 2014-11-20T17:17:19.480

@TreyThomas Oh I see - so Ruby needs an extra level of escape here – Digital Trauma – 2014-11-20T17:23:11.150

8

Python 3 106

s=__import__('socket').socket(2,1) 
s.bind(('',80))
s.listen(9)
while 1:s.accept()[0].send('HTTP/1.1 418\n')

Tuomas Laakkonen

Posted 2014-11-19T12:42:16.453

Reputation: 341

import socket;s=socket.socket(2,1) and s=__import__('socket').socket(2,1) both have 34 bytes – TheInitializer – 2015-12-06T01:21:36.697

1

Based on hallvabo's comment in Tips for golfing in Python, from socket import*;s=socket(AF_INET,SOCK_STREAM) is shorter.

– manatwork – 2014-11-19T19:30:54.480

@manatwork Thanks, is there a shorter way to infinite loop? I didn't find one in the tips... – Tuomas Laakkonen – 2014-11-19T19:33:29.087

3while 1:s.accept()[0].sendall(bytes('HTTP/1.1 418\n','UTF8')) – unless I missed something. (By the way, feel free to count newlines a single characters, as the language allows them. That way you loose nothing by separating the commands by newlines instead of ;.) – manatwork – 2014-11-19T19:39:02.213

You can cut off 2 characters by assuming port 80 is accessible, according to the question owner's comment. One ugly thing which may break portability, but may be acceptable here: use the constants' values directly s=socket(2,1) (at least those are their values on my Linux).

– manatwork – 2014-11-20T11:46:23.400

The documentation says for listen()'s parameter that “the maximum value is system-dependent (usually 5)”. So instead of 10, 9 is more that enough and 1 character shorter. And instead of bytes('HTTP/1.1 418\n','UTF8') a literal b'HTTP/1.1 418\n' is enough. Or if you make the code Python 2, then the b bytesprefix is not needed anymore. And the shorter send() also seems to be enough. – manatwork – 2014-11-20T17:32:54.030

106: s=__import__('socket').socket(2,1);s.bind(('',80));s.listen(9) while 1:s.accept()[0].send('HTTP/1.1 418\n') – TheDoctor – 2014-11-26T02:04:53.993

8

Node.js, 80

require('http').createServer(function(q,s){s.writeHead(418);s.end()}).listen(80)

The response is

HTTP/1.1 418 I'm a teapot
Date: Wed, 19 Nov 2014 21:08:27 GMT
Connection: keep-alive
Transfer-Encoding: chunked

0

Qwertiy

Posted 2014-11-19T12:42:16.453

Reputation: 2 697

2I find it just awesome that node supports that error code natively ^^ – Levite – 2015-02-25T07:31:33.767

7

Haskell - 142 bytes

import Network
import System.IO
main=listenOn(PortNumber 8888)>>=f
f s=do{(h,_,_)<-accept s;hPutStr h"HTTP/1.1 418\r\n";hFlush h;hClose h;f s}

Taylor Fausak

Posted 2014-11-19T12:42:16.453

Reputation: 761

5

Tcl (>= 8.5), 78 bytes

Edit - added in an extra newline (total of 2 newlines) for the sake of compliance.

socket -server {apply {{c - -} {puts $c "HTTP/1.1 418
";close $c}}} 80
vwait f

Digital Trauma

Posted 2014-11-19T12:42:16.453

Reputation: 64 644

A response must end with a newline, see w3.org/Protocols/HTTP/Response.html – Nit – 2014-11-20T06:58:01.173

3

@Nit - Yes, Tcl puts will automatically append a newline, unless the -nonewline option is given. https://www.tcl.tk/man/tcl/TclCmd/puts.htm

– Digital Trauma – 2014-11-20T16:05:06.603

5

Powershell, 398

$Listener = New-Object System.Net.Sockets.TcpListener([System.Net.IPAddress]::Parse("10.10.10.10"), 80)
$Listener.Start()
while($true)
{
    $RemoteClient = $Listener.AcceptTcpClient()
    $Stream = $RemoteClient.GetStream()
    $Writer = New-Object System.IO.StreamWriter $Stream
    $Writer.Write("HTTP/1.1 418 I'm a Teapot`nConnection: Close`n`n")
    $Writer.Flush()
    $RemoteClient.Close()
}

258

$l=New-Object System.Net.Sockets.TcpListener([System.Net.IPAddress]::Parse("10.10.10.10"),80);$l.Start();while($true){$r = $l.AcceptTcpClient();$s = $r.GetStream();$w = New-Object System.IO.StreamWriter $s;$w.Write("HTTP/1.1 418`n`n");$w.Flush();$r.Close()}

user2320464

Posted 2014-11-19T12:42:16.453

Reputation: 151

5

Julia: 86 73 characters

s=listen(80)
while 1<2
c=accept(s)
write(c,"HTTP/1.1 418

")
close(c)
end

manatwork

Posted 2014-11-19T12:42:16.453

Reputation: 17 865

I don't think you need the actual "I'm a teapot" part -- the response code should be enough. – Desty – 2014-11-21T10:22:55.910

Yes, I noticed it. But as that won't help much on Julia's score I kept it complete. But probably would be better to remove it to facilitate comparison of languages. – manatwork – 2014-11-21T11:08:07.753

5

R, 80 characters

Never did socket programming with R before, but I'll give it a try:

repeat{s=socketConnection(,80,T,open="r+");cat("HTTP/1.1 418\n",file=s);close(s)}

Here socketConnection opens a socket: first argument should be the host, the default being localhost we can skip it here; the second argument is the port which has no default, then argument server when specified TRUE creates the socket, if FALSE it just connects to an existing one. T is, by default, equals to TRUE, in R.

Edit: As suggested in a suggested edit by @AlexBrown, this could be shorten into 69 characters:

repeat cat("HTTP/1.1 418\n",file=s<-socketConnection(,80,T))+close(s)

plannapus

Posted 2014-11-19T12:42:16.453

Reputation: 8 610

4

Node.js koa, 61 Bytes

require('koa')().use(function*(){this.status=418}).listen(80)

Response:

HTTP/1.1 418 I'm a teapot
X-Powered-By: koa
Content-Type: text/plain; charset=utf-8
Content-Length: 12
Date: Thu, 20 Nov 2014 07:20:36 GMT
Connection: close

I'm a teapot

Requires node v0.11.12 +

Run as:

node --harmony app.js

c.P.u1

Posted 2014-11-19T12:42:16.453

Reputation: 1 049

What's function*? – nyuszika7h – 2014-11-30T17:53:40.087

2

That's a generator function, part of the ECMAScript 6, Harmony proposal.

– c.P.u1 – 2014-12-01T05:56:18.960

4

Shell + socat, 60

socat tcp-l:80,fork exec:'printf HTTP/1.1\\ 418\\ T\r\n\r\n'

Vi.

Posted 2014-11-19T12:42:16.453

Reputation: 2 644

echo -e HTTP/1.1 418 T\r\n\r is shorter. – jimmy23013 – 2014-11-20T15:02:34.403

The \\ T isn't even needed. – nyuszika7h – 2014-11-30T17:51:59.657

3

You can do this with minimal effort using a .htaccess file and php.

All the accesses to your server will return the status 418.

Below is the code:

.htaccess (28 bytes)

RewriteRule .* index.php [L]

PHP (38 19 bytes)

<<?header(TE,1,418);

Thanks to @primo for saving me a bunch of bytes!


I have tested this and confirm it returns the desired result!

http://i.stack.imgur.com/wLb9p.png

By the way, "Pedido" means "Request" and "Resposta" means "Answer".

Ismael Miguel

Posted 2014-11-19T12:42:16.453

Reputation: 6 797

Old question, but I just noticed that <?header(TE,1,418); will also return status 418 (6 bytes shorter than <?header("HTTP/1.1 418");). – primo – 2017-02-17T10:35:26.650

-1, this is not a complete program. It relies on an external web server. – nyuszika7h – 2014-11-30T17:54:15.457

@nyuszika7h Actually, it relies on Apache with PHP installed as a module. Your argument is both valid and invalid. Apache only redirects the accesses to the PHP file, the PHP file takes care of the code. – Ismael Miguel – 2014-11-30T17:55:20.730

@nyuszika7h If we go down on that layer, then you can't even use the console to run your PHP code. Apache is the starter. The trigger that fires the bullet. Running the PHP file from the console, would make the console the trigger. – Ismael Miguel – 2014-11-30T18:01:34.310

You're relying on having Apache already running, and I don't think it will work without changing the default configuration either. I don't care what you say, there's no way I can see this as being valid. But you should ask @Nit, as it's their question. – nyuszika7h – 2014-11-30T18:02:41.587

@nyuszika7h If it was invalid, the OP would have said already. – Ismael Miguel – 2014-11-30T18:05:20.723

.htaccess is not even code, it's just a limited configuration file. If this is valid (which apparently it is, judging by the fact that the OP hasn't said anything) then I'll eat my hat. Seriously though. – nyuszika7h – 2014-12-05T19:09:24.867

@nyuszika7h Which seasoning you will wish sir? And according to your comment, then my char count should be reduced to 38 bytes, since that is the only programming part with real code. – Ismael Miguel – 2014-12-05T19:15:36.110

That alone isn't valid, someone else already tried that. – nyuszika7h – 2014-12-05T19:16:13.790

@nyuszika7h Any evidence to support your statement? – Ismael Miguel – 2014-12-05T19:16:44.173

Revision history of said post. But if that isn't enough, I'm sure @Nit will be glad to confirm that the original version of that was invalid. – nyuszika7h – 2014-12-05T19:33:17.157

@nyuszika7h That's just an edit log. Nothing important to see there. – Ismael Miguel – 2014-12-05T19:35:42.620

The comments were deleted because they're obsolete. That's just how Stack Exchange works. Like I said, I'm sure the OP (or the person who wrote that answer) will be glad to confirm. – nyuszika7h – 2014-12-05T19:37:20.053

@nyuszika7h Let me tell you a story. A few days ago, I answered a question on stackoverflow (the rules are almost the same, you will see). And one user decided to "bash" me and the OP, both for our low reputation and me for answering it and the OP for asking it. And then, all the comments where deleted. I posted on meta (I can provide a link) asking what I could do in case of such a situation. Their answer was that without comments, there is nothing that can be done. Here is the same case: if you want me to delete this answer, prove me it is invalid. Deleted comments aren't enough evidence. – Ismael Miguel – 2014-12-05T19:45:12.053

Let us continue this discussion in chat.

– nyuszika7h – 2014-12-05T19:46:00.913

3

MATLAB, 97 86 bytes

Not really a serious contender in terms of absolute byte-count, but I'd like to post it because I didn't think it would be possible to write a fully functional webserver using a mathematical tool. Note the use of property shortening: 'Ne','s' internally expands to 'NetworkRole', 'server'.

t=tcpip('0.0.0.0','Ne','s');while 1
fopen(t)
fprintf(t,'HTTP/1.1 418\n')
fclose(t)
end

Sanchises

Posted 2014-11-19T12:42:16.453

Reputation: 8 530

2

node.js with CoffeeScript (76)

require("connect")().use((q,s,n)->s.writeHead 418;s.end();return;).listen 80

Just compile it to JavaScript, then you need to run npm install connect. After that start it with node server.js

Knerd

Posted 2014-11-19T12:42:16.453

Reputation: 1 251

2

nginx - 35

events{}http{server{return 418;}}

Throw that in nginx.conf, start nginx.

Not sure if this uses standard loopholes "Using built-in functions to do the work" or "Interpreting the challenge too literally." Oops, looks like OP won't like this answer.

zamnuts

Posted 2014-11-19T12:42:16.453

Reputation: 263

+4/-4: well done on controversy :D – cat – 2016-03-20T02:45:30.837

Would it still work if you dropped the ;? – Cyril – 2014-11-23T08:58:30.210

2

Perl, 78

use Web::Simple;sub dispatch_request{sub{[418,[],[]]}}__PACKAGE__->to_psgi_app

run as plackup whatever.pl.

hobbs

Posted 2014-11-19T12:42:16.453

Reputation: 2 403

If you want a plack application, then just sub{[418,[],[]]} should be sufficient. (16 characters.) – tobyink – 2014-11-24T03:57:01.127

Of course you're right! It's not like I'm using any of the framework, why load it? :) – hobbs – 2014-11-24T04:55:05.453

@tobyink feel free to submit it as your own though :) – hobbs – 2014-11-24T04:56:08.870

2

Python 2.7/Django, 94 bytes

(added from default boilerplate from django-admin.py startproject) In urls.py:

import django.http.HttpResponse as r;urlpatterns=patterns(url(r'^*$',lambda q:r(status=418)))

TheInitializer

Posted 2014-11-19T12:42:16.453

Reputation: 829

1

Java 7, 208 bytes

import java.net.*;class R{public static void main(String[]a)throws Exception{for(ServerSocket s=new ServerSocket(80);;){Socket p=s.accept();p.getOutputStream().write("HTTP/1.0 418\n".getBytes());p.close();}}}

This question needed a java answer.

poke@server ~
$ curl -i localhost:80
HTTP/1.0 418

Poke

Posted 2014-11-19T12:42:16.453

Reputation: 3 075

Where is status message? – Qwertiy – 2017-02-28T20:53:20.553

@Qwertiy I think this question only asks for status code which I interpret as the integer, so it follows that the status message/reason is not strictly required. – Poke – 2017-02-28T20:55:46.827

Not me to deside, but I think it should be with status text. – Qwertiy – 2017-02-28T20:58:35.313

3Get Java 8 already >_> – Pavel – 2017-02-28T21:12:06.433

Here it is as a lambda for 148 bytes – Benjamin Urquhart – 2019-07-21T22:22:24.833

1

C# + OWIN 251 240

I was really hoping it'd be shorter, but the long namespaces ruined that plan. Requires the Microsoft.Owin.SelfHost package available on NuGet.

using Owin;class P{static void Main(){Microsoft.Owin.Hosting.WebApp.Start<P>("http://localhost");while(0<1);}public void Configuration(IAppBuilder a){a.Run(c=>{c.Response.StatusCode=418;return System.Threading.Tasks.Task.FromResult(0);});}}

Bob

Posted 2014-11-19T12:42:16.453

Reputation: 844

1

node.js with connect (78)

require('connect')().use(function(q,s,n){s.writeHead(418);s.end()}).listen(80)

You need to run npm install connect first. Then start it with node server.js

Knerd

Posted 2014-11-19T12:42:16.453

Reputation: 1 251

1

Go, 162 bytes

package main
import "net/http"
func main(){http.HandleFunc("/",func(w http.ResponseWriter,r *http.Request){w.WriteHeader(418)})
http.ListenAndServe(":80", nil)
}

ashooby

Posted 2014-11-19T12:42:16.453

Reputation: 69

1

Factor, 101 141 bytes

[ utf8 <threaded-server> readln 10 base> >>insecure [ "HTTP/1.1 418\r" print flush ] >>handler [ start-server ] in-thread start-server drop ]

Return 418 to everyone who connects.

cat

Posted 2014-11-19T12:42:16.453

Reputation: 4 989