\$\begingroup\$
\$\endgroup\$
3
Make the shortest proxy server.
Input/Output Specifications
Client:
- Input: a port number (32-bit integer) (,) or (space) a url (see test cases)
- Output: html source of the url
Server:
- Input: port to listen to (32-bit integer)
- Output: REC when input is received from the client, OK when finished sending the html source to the client.
Rules
- You may make either a complete program or functions in an interpreted language.
- Input is taken through stdin or given as a arguments.
- Your code has to work for the given test cases only (more is optional, less is invalid).
- The client output has to be the same as with what you get when you "view source" from a browser.
- The server may exit after each handled request (i.e does not have to be persistent/daemon)
- (edit) Any other input/output than what specified, is forbidden.
Test cases (client inputs)
- N1 https://stackoverflow.com/
- N2 http://en.wikipedia.org/wiki/Proxy_server
- N3 http://stackexchange.com/search?q=code+golf
where Ni are random integers between 1 and 2048.
asked Mar 13, 2011 at 20:55
-
\$\begingroup\$ Does the server have to be reusable? Or can it be a one-shot proxy? \$\endgroup\$Nemo157– Nemo1572011年03月13日 21:29:59 +00:00Commented Mar 13, 2011 at 21:29
-
\$\begingroup\$ @Nemo: what do you mean reusable? (but I suppose the answer is "yes it can be a one-shot") \$\endgroup\$Eelvex– Eelvex2011年03月13日 21:41:39 +00:00Commented Mar 13, 2011 at 21:41
-
\$\begingroup\$ @Nemo: yes, it is ok if the server exits after a well handled request. \$\endgroup\$Eelvex– Eelvex2011年03月13日 21:59:25 +00:00Commented Mar 13, 2011 at 21:59
2 Answers 2
\$\begingroup\$
\$\endgroup\$
3
ZSH - 57 + 42 characters
Server:
s=$(nc -l 1ドル)
echo REC
curl -s $s|nc 127.0.0.1 1ドル
echo OK
Client:
echo 2ドル|nc 127.0.0.1 1ドル
echo "$(nc -l 1ドル)"
Usage:
sudo zsh server.zsh 123
sudo zsh client.zsh 123 http://stackoverflow.com/
sudo zsh client.zsh 123 http://en.wikipedia.org/wiki/Proxy_server
sudo zsh client.zsh 123 "http://stackexchange.com/search?q=code+golf"
answered Mar 13, 2011 at 21:54
-
\$\begingroup\$ It's been a while since I last used zsh, but can't you get rid of all the quotes (necessary for bash) when using that shell? \$\endgroup\$PleaseStand– PleaseStand2011年03月13日 21:58:30 +00:00Commented Mar 13, 2011 at 21:58
-
\$\begingroup\$ @idealmachine: The ones on the second line of the client I think are necessary, I was only getting the footer back from the StackExchange search. The other ones, I'm not sure about, seems to work without them though. I don't really do much scripting in zsh. \$\endgroup\$Nemo157– Nemo1572011年03月13日 22:05:55 +00:00Commented Mar 13, 2011 at 22:05
-
\$\begingroup\$ Yes, curl should be silenced (updated rules to clarify). \$\endgroup\$Eelvex– Eelvex2011年03月13日 22:31:22 +00:00Commented Mar 13, 2011 at 22:31
\$\begingroup\$
\$\endgroup\$
Nim, 153 + 101 bytes
Server:
include httpclient
proc(n=0)=(var s,c=newSocket();s.bindAddr n.Port
s.listen
s.accept c;echo"REC"
c.send newHttpClient().getContent(c.recvLine);echo"OK")
Client:
import net
proc(n=0,u=""):string=(var c=newSocket();c.connect "*",n.Port
c.send u&"\n"
c.recv 999999)
Usage:
let s = <server proc>
s(2034)
let c = <client proc>
echo c(2034, "https://stackoverflow.com/")
$ nim r -d:ssl server.nim & (sleep 2s; nim r client.nim)