r/dailyprogrammer • u/jnazario 2 0 • Dec 15 '17
[2017-12-15] Challenge #344 [Hard] Write a Web Client
Description
Today's challenge is simple: write a web client from scratch. Requirements:
- Given an HTTP URL (no need to support TLS or HTTPS), fetch the content using a GET request
- Display the content on the console (a'la curl)
- Exit
For the challenge, your requirements are similar to the HTTP server challenge - implement a thing you use often from scratch instead of using your language's built in functionality:
- You may not use any of your language's built in web client functionality or any third party library or tool. E.g. you can't use Python's
urllib,httplib, or a third-party module likerequestsorcurl. Same for any other language and their built in features; you may also not shell out to something likecurl(e.g. nosystem("curl %s", url)). - Your program should use string processing calls to dissect the URL (again, you cannot use any of the built in functionality like Python's
urlparsemodule or Java'sjava.net.URL, or third-party URL parsing libraries like HTParse). - Your program should support non-standard ports (for instance http://server.io:8080/).
- Your program does NOT need to support TLS or SSL.
- Your program should use low level
socket()calls (or equivalent) to connect to the server, and make a well-formatted HTTP/1.1 request. That's the whole point of the challenge!
A good test server is httpbin, which can give you all sorts of feedback about your client's behavior; another is requestb.in.
Example Output
Here is some simple bare-bones output from httpbin.org:
HTTP/1.1 200 OK
Connection: keep-alive
Server: meinheld/0.6.1
Date: Fri, 15 Dec 2017 17:14:03 GMT
Content-Type: application/json
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
X-Powered-By: Flask
X-Processed-Time: 0.00114393234253
Content-Length: 158
Via: 1.1 vegur
{
"args": {},
"headers": {
"Connection": "close",
"Host": "httpbin.org"
},
"origin": "1.2.3.4",
"url": "http://httpbin.org/get"
}If your client can emit that kind of thing to standard out, you're set.
Bonus
The above focuses on a simple client. Here are a few more things you can do to extend it:
- Support POST requests (and feeding the data)
- Support authentication
- Support arbitrary additional headers or overwriting headers
14
u/jnazario 2 0 Dec 15 '17
very basic Python 2 solution
3
u/ghost20000 Dec 15 '17
Can you give an example for the output?
3
u/AndrewBregger Dec 24 '17
My Rust solution.
It takes more time than expected to read the response from the server. Requesting www.cnn.com takes 600 seconds to read the entire response.
Any input to make it better is appreciated.
2
u/mn-haskell-guy 1 0 Dec 16 '17 edited Dec 16 '17
perl + netcat:
8
u/[deleted] Dec 16 '17 edited Dec 16 '17
C
Here's my attempt in C. I'm sure it's atrocious, but I learned a great deal making it. Fun challenge. Picked up a lot by following along with this article.
The url dissection is pretty weak, lol, and breaks if there's more than one forward slash following the url. Criticism is definitely welcomed.
Edit: I don't think I broke any rules, but I could be wrong.
Edit2: Rewrote the url dissector (after picking up some things from /u/zomgreddit0r's solution). It actually handles more than one forward slash now!
Code:
Output
6
u/afronut Dec 15 '17 edited Dec 15 '17
Rust solution. Feedback welcome. Tear it apart :).
1
u/mn-haskell-guy 1 0 Dec 16 '17
Do we have to handle redirects?
3
u/Daanvdk 1 0 Dec 16 '17 edited Dec 16 '17
Python3
3
u/Hydrolik Dec 16 '17
Julia
I have no experience with web related stuff, so I hope this is as low level as requested. No bonus.
Output:
2
u/millertime643 Dec 17 '17
Python 3
2
u/mochancrimthann Dec 21 '17 edited Dec 24 '17
Javascript with POST and header override bonuses
EDIT: Parses nested paths.
2
u/cdrootrmdashrfstar Dec 22 '17
Python 3.6
Here's my attempt to make something similar to Request's get:
Successful output:
2
u/CraftersLP Dec 23 '17
a quick php solution
2
u/rabiddev Dec 29 '17
Scala
1
u/line_over Dec 25 '17
Python3.6
1
u/primaryobjects Dec 27 '17 edited Dec 27 '17
R
Gist
Output
1
u/hi_im_nate Jan 23 '18 edited Jan 29 '18
Very simple Rust solution. For some reason, it doesn't work with httpbin.org, but it does work with other sites that I've tested. Google, Facebook, Github... It fails on httbin with a 505 HTTP Version Not Supported error. This error does not occur when I copy and paste the exact request into a telnet session, so I don't know what's up with that.
EDIT: I figured out the problem, I was using normal line endings (\n), but I need to use CRLF (\r\n). I also updated it to support the http_proxy env variable
1
u/do_hickey Jan 26 '18
Python 3.6
I'm sure I missed a few booboos that can cause errors, but I tried my best to handle the basics. If you notice any issues or ways to make it better, let me know! A bit lengthy due to all of the different types of URLs handles.
Source:
Sample Output:
1
u/Sorry4StupidQuestion Mar 13 '18
Late on my entry, just found this sub.
https://github.com/heyitswither/pyhttp
-2
u/cheers- Dec 15 '17 edited Dec 15 '17
Node
requires full urls (protocol + hostname) otherwise it wont parse. A bit primitive but it works.
0
u/[deleted] Dec 15 '17
[deleted] — view deleted comment
0
u/[deleted] Dec 16 '17
[deleted] — view deleted comment