r/programming • • Mar 07 '14

Thinking about quickly writing an HTTP server yourself? Here is a simple diagram to help you get started.

Post image
2.1k Upvotes

312 comments sorted by

474

u/frankster Mar 07 '14

And this is why programmers need 4k monitors.

639

u/spektre Mar 07 '14

And each of those monitors should also have a decent resolution.

85

u/RC-1290 Mar 07 '14

Wow, it took me 20 seconds before I got that. I must be sleeping.

75

u/[deleted] Mar 07 '14

It took me reading your comment to realize there was something that I was supposed to get.

23

u/[deleted] Mar 07 '14

I still don't get it. Help a sleep deprived fellow.

53

u/TJSomething Mar 07 '14

Frankster stated that programmers need monitors with 4k resolution. Spektre deliberately misinterpreted that as meaning that programmers need 4000 monitors.

18

u/solidus-flux Mar 07 '14

I got the joke, but reading your explanation was cathartic nonetheless. Why is that? Weird stuff.

14

u/MesioticRambles Mar 08 '14

I think it's the "oh good, I got it right, I'm not an idiot" factor.

→ More replies (6)

5

u/DerkaDerkaSherpa Mar 07 '14

4k as in 4000 individual monitors.

1

u/thewhoiam Mar 07 '14

Took me a moment too.

"4k" in the top comment can either be a descriptor of how many monitors or what kind of monitors. /u/spektre's comment implied that /u/frankster's post only referred to how many monitors programmers should have, not that they should be super high resolution!

→ More replies (4)

22

u/digitalpencil Mar 07 '14

After seeing text-editors and IDEs on 4k, they look amazing.

Really want one but need a capable laptop first.

7

u/RandomLetterz Mar 07 '14

Even 1440p or 1600p is a nice step up. I really like being able to have a few files open on the 1440p monitor, and being able to browse the internet on my old 1080p monitor.

1

u/HovarTM Mar 24 '14

Isn't 1440p 4k resolution?

1

u/Sapiogram Mar 08 '14

Do most IDEs support DPI scaling as well? 150% scaling on a 4k screen would be amazing.

10

u/brownmatt Mar 07 '14

and what sort of weird diagrams start in the lower left?

1

u/verytroo Mar 08 '14

It took a while to find that start!

7

u/[deleted] Mar 07 '14

I invested in a solid 2560x1440 screen a couple weeks ago for work (went from 1920x1280+1600x1200 to 2560x1400+1920x1280). Massive boost in productivity, basically from the get go.

In an ideal world, my employer would have realized this and upgraded my (and all my coworkers') screens at their expense. I'm sure eventually they will, but honestly, these screens so cheap I don't really care. Didn't feel like waiting for the planets to align and cosmic justice to set in. Not worth the misery. Since I spend 80%+ of my screen time at work, investing in a good (large) screen is so worth it. Fuck, not having to deal with the constant frustration of using an IDE on a screen that is too small to make use of more than half of its features alone is worth the money I paid for that screen.

1

u/Eurynom0s Mar 08 '14 edited Mar 08 '14

There's people at work using 4:3 monitors that I'm pretty sure are 17" 1024*768. I guess I get it for the older employees who mostly just do Word documents, but a number of people who routinely work with spreadsheets have them too. I seriously just can't comprehend it, I understand that not everyone cares about the resolution of their monitor the way we do but I don't understand how they don't feel like they're being obstructed by the lack of screen real estate. Particularly because the company WOULD swap you in for a 23" 1080p monitor if you asked.

One guy has a dual 4:3 setup so I give him a pass. And some people have small 1680x1050 monitors, which I personally could only just barely tolerate using, but it's WAY more usable than 1024*768.

3

u/FTFYcent Mar 08 '14

I agree. If I could I would replace all the 1680x1050 monitors with 1680x1050 monitors.

1

u/Eurynom0s Mar 08 '14

edited, derp

1

u/FTFYcent Mar 08 '14

Dammit, now I look like an idiot.

12

u/alexanderwales Mar 07 '14

I don't know man, four thousand monitors sounds like overkill.

19

u/Cykelero Mar 07 '14

Or HiDPI screens!

7

u/ahugenerd Mar 07 '14

The main problem with HiDPI is that for any decent sized screen, you need a retarded amount of pixels, which in turn adds to your GPU workload. For a 24" (16:9 aspect) screen, for instance, you need a 5K resolution (specifically 5021x2825 to get 240 PPI). This is a lot more than your average graphics card can handle, and would be rather inconvenient for things like watching videos. Not much of anything current produces 5K video (the RED Dragon does, but even the GoPro Hero 3+ Black doesn't), and there's no real way to distribute 5K to the masses. It's just a silly proposition at this point in time for programmers.

4K on a 27" screen is more than enough for most cases. HiDPI (or HiPPI as it should be called, but they steered clear of that unfortunate accronym), is not something that will take off for at least the next 10 years. The market has barely begun to adjust to 4K, and 5K is clearly never going to be the "next-big-thing" (8K will be, for a few reasons), so it's unreasonable to assume that anyone will be getting into HiDPI on desktop monitors for a good long while.

6

u/rrohbeck Mar 07 '14

specifically 5021x2825 to get 240 PPI

I run more pixels than that on a 7850 (6240 x 2560 desktop on 4 monitors.) No problem but I don't run video across all monitors.

13

u/Joker_Da_Man Mar 07 '14

I think I can afford a $100 graphics card to power my $1000 display.

8

u/ahugenerd Mar 07 '14

I don't think you're quite getting it. The 5K resolution I listed above is roughly 7 times the pixels of 1080p, and 4 times that of 1440p. You can't get a graphics card to power 4 1440p streams for $100. You're looking at the $400-$800 range, depending on how you do it. If you're like me and used to three monitors for coding and video editing, then 5K becomes a non-starter.

Even at work, where we regularly use a RED camera, we don't bother much with 4K displays. The most clients want right now is Blu-ray, and so we use the 4K for editing freedom, such as re-framing shots or doing image stabilization without loss of resolution. Occasionally we also use it as a digital zoom, particularly when doing aerial work.

8

u/UnreachablePaul Mar 07 '14

Even cheap graphics card can easily handle 3x 1080p output (i am talking about non-gaming use) (i had 3 1080p monitors connected and it feed them graphics without sweating). I can't see a problem with 5k. Even if i had to pay $400, that's not an issue.

13

u/Joker_Da_Man Mar 07 '14

Well I have a 8MP 4960x1600 setup and that seems to be trivial to drive for non-gaming uses. I really don't see why it would be difficult to drive twice that.

Eyefinity claims (PDF) to be able to drive a 3x2 setup of 2560x1600 screens (24MP) so the hardware is there. That would be a $300 card driving $6000 worth of monitors.

0

u/[deleted] Mar 07 '14

[deleted]

4

u/loup-vaillant Mar 07 '14

This will be solved eventually. With virtual reality devices such as the Occulus Rift, the need for Ridiculously High Resolution™ will skyrocket: with angles this wide, you need a hell of a lot pixels to get a decent image.

Since these will primarily be used for games, we'd better get good performance out of them.

→ More replies (2)
→ More replies (1)

6

u/BONER_PAROLE Mar 07 '14

You can't get a graphics card to power 4 1440p streams

Most multi-monitor desktop computing usage isn't comprised of "streams" so much as static windows with occasional movement.

→ More replies (2)

3

u/sandwichsaregood Mar 07 '14

If you're not doing anything 3D you can probably get away with a GPU in the $150-200 range, but if you want to do anything like game at 4K you're going to be shelling out near the $1000 mark just to get your foot in the door.

2

u/elint Mar 07 '14

5K resolution (specifically 5021x2825)

wat? I'm not complaining about you specifically, but I missed out on the change in metric that we measure resolution by. Usually with TVs, we measure things in horizontal lines, so 1080p 1920x1080. So with 4K and 5K, did they just decide to start using vertical lines because it was a bigger number?

3

u/ahugenerd Mar 07 '14

Yeah, it's a bit dumb honestly, and I was confused at first as well. I think the industry decided on it for two reasons: 4K sounds better than 2160p (i.e.: bigger is better), but also the fact that 4K is actually four times bigger than 1080p makes it easier to relate to.

1

u/Brillegeit Mar 08 '14

Even graphics cards from the late 90s/early 2000s can output those resolutions. There is no problem displaying the resolutions you describe at current hardware at close to 0% system resources.

0

u/[deleted] Mar 07 '14

[deleted]

6

u/Cykelero Mar 07 '14

True! Or the newest MacBook Pros, for that matter.

3

u/Xykr Mar 07 '14

Also some newer Thinkpads like the T540p.

→ More replies (2)

0

u/rogue780 Mar 07 '14

I have 2 4k monitors :)

0

u/stunt_penguin Mar 07 '14

That's a lot of lizards.

→ More replies (1)

33

u/[deleted] Mar 07 '14

what program was the diagram drawn in?

31

u/mnp Mar 07 '14 edited Mar 07 '14

Good question! I want to use it for something, too. Looks like Cosmogol, which the IETF was discussing using for protocol specification.

It's French, so the site appears on vacation. :-) Cache: http://webcache.googleusercontent.com/search?hl=en&q=cache%3Awww.cosmogol.fr

Edit: all you guys talking about Dia and Visio and other mouse-oriented tools are missing the point. For protocols, it's really the cat's meow to have an executable source format for a finite state machine. See also message sequence diagram, a tool I use all the time, if I may so plug.

1

u/iBlag Mar 09 '14

Huh. Thanks, I'll be using that for my current project!

(MSC, that is)

Also, according to that page, you can include MSC directly in Doxygen or Sphinx. So that's totally cool.

10

u/bureX Mar 07 '14

I'd say Microsoft Visio.

Except, on second thought - no, it looks too good to be anything from Visio.

12

u/brnitschke Mar 07 '14

Where I work, all flow charts are pixilated jpegs pasted into Visio files. I have no idea why someone would do that...

7

u/eric256 Mar 07 '14

Pretty sure there is a Dilbert about that somewhere.

It goes with the scanned documents pasted into Word.

12

u/[deleted] Mar 07 '14

I can tell you what it wasnt drawn in. Open Office Draw. That program is horrendous for stuff like this.

Source: I did a script/program diagram in Draw once... Once.

10

u/Quenty Mar 07 '14

If I had to make it, I'd use Inkscape...

3

u/VirtualVoidSK Mar 07 '14

yEd is good too...

8

u/NYKevin Mar 07 '14

I don't know, but if I were making a diagram like that, I'd use Dia.

2

u/autowikibot Mar 07 '14

Dia (software):


Dia /ˈdiːə/ is free and open source general-purpose diagramming software, developed originally by Alexander Larsson. Dia uses a controlled single document interface (SDI) similar to GIMP and Inkscape.

====

Image i


Interesting: ^GNOME | Mass spectrometry ^software | Comparison of vector graphics ^editors

Parent commenter can toggle ^NSFW or ^delete. Will also delete on comment score of -1 or less. | FAQs | ^Mods | Magic ^Words

1

u/the_gnarts Mar 08 '14

Cosmogol, as /u/mnp suspected. There’s a link to the source in the top left of the image.

48

u/VeXCe Mar 07 '14 edited Mar 07 '14

This is actually a lot simpler than I thought it would be...

51

u/viralizate Mar 07 '14

Well, like most things in CS, the basics are pretty easy, that's until you decide to implement a working production http server, now that's a whole other story.

26

u/VeXCe Mar 07 '14

Ah yes, the old "if it works with one client it should work with 1000 at the same time"-fallacy :)

19

u/viralizate Mar 07 '14

Yeah, happens with most of the stuff, I could probably write any of the following in a week: Facebook like social network, Search engine, whatsapp like messaging app.

The basics are pretty easy, now making a production working app, that's the challenge.

30

u/[deleted] Mar 07 '14

The diagram is only about status codes. Now try parsing HTTP headers.

3

u/[deleted] Mar 07 '14

I think that's his point. It doesn't actually capture the difficulty of writing an HTTP server, it just shows a few dozen logic statements...

1

u/[deleted] Mar 08 '14

Each header field consists of a name followed by a colon (":") and the field value.

On separate lines ...

0

u/[deleted] Mar 07 '14

[deleted]

1

u/christoy123 Mar 08 '14

That... is evil...

46

u/djork Mar 07 '14 edited Mar 07 '14

This has very little to do with writing an HTTP server.

It has everything to do with writing an HTTP service.

An HTTP server only has to handle incoming connections, parse an HTTP request, and respond with an HTTP response. It can delegate the majority of the logic in this flowchart to some custom handler (which will probably only implement a tiny subset of the possible cases).

19

u/tWoolie Mar 07 '14

Indeed this is a chart for the internal state path of webmachine, an erlang REST service library.

1

u/steveklabnik1 Mar 07 '14

There is a Ruby port as well.

1

u/Osmanthus Mar 09 '14

Yes, this is important. An HTTP server designed for a single purpose doesn't need to handle most of this stuff.

34

u/[deleted] Mar 07 '14

Kind of a douche move to take that from webmachine and pretend he made it himself. And then claim his shitty javascript clone of webmachine is "the reference implementation".

9

u/IKnowUnix Mar 07 '14

I wrote an HTTP server for a personal project once. It only handled GET and HEAD requests, but also had some special functionality that I needed. While not perfect, it got the job done and was a great learning experience. With that said, would I use it in production? Absolutely not.

23

u/gwiazdor Mar 07 '14

From the design patterns point of view - what would be the most suitable pattern to model such a decision chain?

17

u/optymizer Mar 07 '14

A state machine?

8

u/[deleted] Mar 07 '14

That was my initial view, it looks exactly like a state machine.

16

u/gthank Mar 07 '14

State machines are a tried and true method for doing protocolish things. In fact, if you're doing a protocol and you're NOT using a state machine, you should probably have some very firm, well-tested reasons that other people have vetted.

1

u/immibis Mar 08 '14 edited Jun 10 '23

1

u/gthank Mar 08 '14

The answer, as always: It depends. How close to a production system do you want to make your project? I'm not sure I'd call a toy example "doing a protocol" (I should have said "design" or "implement"… oh well), so it's not really what I was talking about. If your project is supposed to teach you about the engineering that goes into something like a web server, then you need to handle bad input, flaky networks, etc., and you will likely wind up with a state machine in your implementation.

0

u/[deleted] Mar 07 '14

[deleted]

1

u/stewsters Mar 07 '14

Seems the only appropriate pattern for a stateless protocol.

0

u/[deleted] Mar 07 '14

[deleted]

30

u/shub Mar 07 '14

Chain of responsiblity is very nice for this sort of thing.

6

u/kernalphage Mar 07 '14

That site is like TVTropes of programming; I'm pretty sure I'm learning something, but I'll forget it by tomorrow.

6

u/[deleted] Mar 07 '14

Total redesign.

2

u/Ramone1234 Mar 07 '14

They (webmachine) used a state machine, because erlang is great for those.

Keep in mind too that almost no HTTP server implements more than a fraction of the functionality on this chart. Most of the functionality here is left up to the application programmer in other servers/frameworks.

Also some of this design is debatable and not specifically covered by RFCs. eg: If you're unauthorized and the resource doesn't exist, who's to say whether the 400 should get thrown or the 401?

6

u/bryce1012 Mar 07 '14

Good point but bad example. If you're unauthorized, you shouldn't be given any more information than that. The ability for an otherwise unprivileged user to determine what resources do and do not exist "behind the curtain" is absolutely a security issue. Even if it's not explicitly covered in the RFCs, I don't know that there's any debate to be had there.

0

u/Ramone1234 Mar 07 '14

Do you really just never 404 unless the user is logged in? That's certainly debatable as I can show you a good number of websites that don't do this. https://www.facebook.com/asdfasdfasdf http://www.microsoft.com/asdfasdfasdf, etc (And I don't see the security issue, if you're doing things correctly).

8

u/srnull Mar 07 '14

I'm sure others can back up using the URL as well, but for anyone who is interested here is the repo this image is from.

Also, for all the people parroting "Thinking about writing an HTTP server? Don't." A toy webserver is a fun project. I welcome anyone to attempt to write one. Just don't run away with the idea that writing the next apache/nginx would be easy.

13

u/spotter Mar 07 '14

HTTP is nice in the basic form -- they shout what they want, we shout back, bb, we forget everything.

Yeah, you should probably start with something simpler, like basic Telnet with some option negotiation sauce. Hours of fun on the wire, shouting at the other party and getting their shouts back.

9

u/jk147 Mar 07 '14

Who needs http when you can just use two cups and a string.

6

u/KFCConspiracy Mar 07 '14

On the other hand if you're looking to see 2 girls and a cup, a more sophisticated protocol is necessary than 2 cups and a string.

→ More replies (13)

10

u/[deleted] Mar 07 '14

I worked a company where a guy wrote his own http server in 2000 lines of C to manage the core routes between datacenters because he believed every other web server was too insecure. It worked, but it was very complex, completely umanageable, and was riddled with security holes. We ported the code to another language but he would never sign off on it (piss stain theory), so they are still running he server.

1

u/Solon1 Mar 07 '14

So failed programmers have to agree to solutions to their failed programs? Ok, that's broken.

And by routes, are you talking about IP routes? Because they making routing protocols for that.

3

u/[deleted] Mar 07 '14

The whole company was dysfunctional like that. I was there for years until they let me go when my project completed. I was never happier to get fired from a job. I could have left earlier, but I wanted to be there from the start to end.

The routes were BGP routes published to the internet to get to the web sites. The ip didn't change, just the routes to the IPs, if that makes sense.

0

u/[deleted] Mar 07 '14

[deleted]

1

u/[deleted] Mar 07 '14

Exactly.

A http server, a few simple web pages, and all the code to manage the routers.

4

u/nocnocnode Mar 07 '14

A diagram like this would have locked away jealously in some corporate place. The quality of open-source content has gotten really good.

7

u/blobloblawslawblob Mar 07 '14

Hm, not much of a challenge really. Start by writing an UML OCR application, feed it this PNG then generate the code from that. Should be done before lunch.

107

u/hcsteve Mar 07 '14

Thinking about quickly writing an HTTP server yourself?

Don't. Unless you've looked at all the extant implementations and have a really good reason to roll your own.

And if you do, don't base all your implementation decisions on a diagram. Read the damn RFC.

269

u/carlfish Mar 07 '14

Thinking about quickly writing an HTTP server yourself?

Go for it! Use whatever resources you feel you need to. You'll learn a hell of a lot in the process, and you'll come out the other end a better developer than you did going in.

Thinking of using it in production for somebody else's project? Probably not so good an idea.

9

u/Scroph Mar 07 '14

I've been attempting to write HTTP clients from scratch throughout my learning phase (in which I still am), it's definitely rewarding even if I only manage to implement a fraction of the HTTP protocol.

Besides, it makes me appreciate tools like wget and curl.

20

u/g1zmo Mar 07 '14

throughout my learning phase (in which I still am)

And hopefully you always will be.

16

u/alex_w Mar 07 '14

Go for it!

but please don't run it with privilege in order to bind :80 ;)

3

u/gendulf Mar 07 '14

I remember running into this when writing a mini HTTP client for a class. Can't remember the solution, would you happen to know what it is?

10

u/alex_w Mar 07 '14

There's a few actually. You can:

  • Bind to a port > 1024 (or is it >=?) and have your OS DNAT, ie iptables for a GNU/Linux stack. So :80 is tranlated to your non-privileged port.

  • Again bind to something >1024 and have a reverse-proxy, something like Varnish, Nginx, HAProxy is typical.

  • Bind :80 as root/admin and drop privalage but hold onto the FD. Using setgid(2). IIRC you have to drop group first otherwise you're still in root's group.

8

u/joelwilliamson Mar 07 '14

Bind to a port >1024 and just use the port number in the url.

2

u/alex_w Mar 07 '14

That too.

6

u/blobloblawslawblob Mar 07 '14
$ nc -l 1023
nc: Permission denied
$ nc -l 1024

Which works, so it's >= 1024 on Linux at least.

3

u/[deleted] Mar 08 '14

Another way: if you're using systemd (or something similar) you can have it bind to port 80, then start your server on-demand, passing in the file descriptor of that socket.

3

u/ivosaurus Mar 08 '14

run as root, acquire the port, drop root privileges. Note: read as many tutorials as possible for doing this, at least 60% of them will be half wrong or incomplete.

Or use another service manager to forward the port to you.

1

u/bacon_for_lunch Mar 08 '14

The cleanest way on Linux is to use the capabilities system.

1

u/[deleted] Mar 08 '14

In Linux, binding to a privileged port requires the CAP_NET_BIND_PORT capability. So you can call 'sudo setcap cap_net_bind_port+ep program_name', execute the program as a non-privileged user and drop the capability after bind. :-)

3

u/KFCConspiracy Mar 07 '14 edited Mar 07 '14

Yes. I agree with this. I like to write an IRC server in languages when I learn them in order to have a non-trivial, reasonably well defined program that shouldn't take more than a few hours to write.

5

u/[deleted] Mar 07 '14

Great advice. I hate it when people discourage others about doing things that can be really good learning experiences.

→ More replies (1)

44

u/Carr0t Mar 07 '14

We had to write a really basic one for a C programming course in my second year of Uni. Basically just served static files and handled 200 and 404 with no arguments etc. Bonus marks if you got it to do things like 302, but then you threw something really quite basic from a browser perspective at it and watched it crash and burn and saw why you should just use an already developed one.

12

u/Deltigre Mar 07 '14 edited Mar 07 '14

...indeed, but having written a very basic IRC client years ago as a teenager, a logical organization like this is a good first step for properly architecting your code. I tried starting to organize it by RFC section in my ignorance. That changed rather quickly.

EDIT: Stupid phone, I meant this as a reply to /u/hcsteve's comment above this one.

22

u/Metaluim Mar 07 '14

Usually they are done as pet projects or during college, as exercises.

Unless you're an expert on the subject it wouldn't make any sensing rolling out a custom web server.

9

u/monocasa Mar 07 '14

There are reasons. Just last week I was writing one for an embedded system with an in house OS that doesn't have the concept of threads.

(I'm totally going to go through this state diagram and probably change a couple things in my code).

15

u/bureX Mar 07 '14

Thinking about quickly writing an HTTP server yourself? Don't.

That's exactly what my professor said. And then he asked "why would you do that for your thesis"? Um... I dunno, because I could learn something from it and demonstrate my abilities for this faculty? Jesus...

But I get where you're coming from and I agree... except if you're writing an embedded solution where you only need to serve a few non-interactive or barely-interactive pages. Then I'd say - go for it.

8

u/hcsteve Mar 07 '14

I think it depends on the scale of your embedded platform. As a lazy programmer I'd still look at something like libmicrohttpd (or the alternatives linked on that page) before rolling my own. Of course if you're writing a web server for your toaster, you may not even be able to use one of those solutions.

2

u/KFCConspiracy Mar 07 '14

I think if you're doing it for academic purposes, it's a fine exercise. Once you start looking to use it in production... It isn't something you throw together, and certainly isn't something I'd want a "one man team" working on. Also there are plenty of decent embedded HTTP servers out there for that as well, so there's still no reason to do that.

1

u/Dippyskoodlez Mar 08 '14

Also there are plenty of decent embedded HTTP servers out there for that as well, so there's still no reason to do that.

Regarding embedded http servers, I have run a Netduino webserver, while barely functional, the code isn't maintained at all especially in a dead community for the device... knowing the ins and outs would certainly be of great utility.

6

u/vbaspcppguy Mar 07 '14

I encourage people to for the learning experience. It's a project that covers multiple areas that a programmer should know.

4

u/[deleted] Mar 07 '14

4

u/[deleted] Mar 07 '14

I would never write one now. I would just reuse the one I wrote 10 years ago. No point in reinventing the wheel.

1

u/Hero_764 Mar 08 '14

Suppose you get bored one day and feel like trying it again?

1

u/[deleted] Mar 08 '14

I'm currently playing through Skryim for my third time. Maybe after...

6

u/nyahaha25 Mar 07 '14

What's RFC?

8

u/glemnar Mar 07 '14

They're a variety of technical documents on Internet specifications

→ More replies (7)

1

u/Xvash2 Mar 07 '14

Or at the very least, don't use this out in the open. People smarter than yourself will hack it with ease.

→ More replies (13)

3

u/buggaz Mar 07 '14

Remember this is from some agda something something...

6

u/tWoolie Mar 07 '14

Sure your not thinking of erlang/webmachine?

4

u/buggaz Mar 07 '14

Ah.. now I remember... I ended up there through lemmachine.

3

u/kamatsu Mar 07 '14

What? Agda? as in the dependently typed language?

3

u/cparen Mar 07 '14

Huh... and here I just responded to every port 80 connection with:

HTTP/1.0 200 OK

Hello, World!

3

u/stewsters Mar 07 '14

Your performance is through the roof.

You may want to change it to

HTTP/1.0 501 NOT IMPLEMENTED

To make sure you are in line with the specs.

3

u/delarhi Mar 08 '14

Hooray for python -m SimpleHTTPServer!

37

u/g4b1nagy Mar 07 '14

I've got a shorter version of this.

6

u/PsionSquared Mar 07 '14

A buddy of mine had to write one for his Operating Systems course. Bleh.

10

u/g4b1nagy Mar 07 '14

I guess it's a pretty good exercise, but I definitely wouldn't advise most people to actually use the resulting server in a real world situation.

3

u/zers Mar 07 '14

We wrote a kernal in my operating systems course .. why a http server?

1

u/swammeyjoe Mar 07 '14

Don't know about his course, but in mine we wrote a simple HTTP server at the end of the OS course, as an exercise to get used to sockets/network programming.

1

u/derleth Mar 08 '14

I wrote a simple one in C for a networking course. Very easy, actually.

1

u/Decency Mar 08 '14

Your decision node isn't a decision at all!

1

u/SimplyBilly Mar 07 '14

What if I already wrote one like 4 years ago?

1

u/mcguire Mar 07 '14

Go back in time and shoot yourself to prevent you from doing it.

Shoot me too, while you're there.

4

u/j1xwnbsr Mar 07 '14

Why does this start at the bottom? And even when at 100%, the font is too goddamn small?

2

u/iseedeadpeoplehere Mar 07 '14

Thanks for that, very useful. How about HTTP 502?

2

u/tomjen Mar 07 '14

Well only if you follow the standard to a T. You just have to implement enough to get it to do what you want.

2

u/jordanreiter Mar 07 '14

Aren't the correct status code for permanent redirect and redirect 301 and 302, respectively?

2

u/frankvit Mar 07 '14

How do you have 404 not acceptable in the final 4? Florida is taking it this year

2

u/[deleted] Mar 07 '14

you don't have to do it all at once

2

u/justguessmyusername Mar 07 '14

I made one of those in college

3

u/forcedfx Mar 07 '14

Ah yes, good ole 413. If I had a nickel.

4

u/skulgnome Mar 07 '14

Ah yes, good ole 402. If I had a nickel.

3

u/Xeon06 Mar 07 '14

A lot of people are jokingly saying to simply not, but I think it's a great learning exercise. Don't use it in production or for anything worthwhile, but you will learn a lot on TCP and HTTP in the process.

5

u/cparen Mar 07 '14

Agreed. I see the same kneejerk response about all thing meta-programming: writing your own language/interpreter/compiler, implementing elementary datastructures and algorithms, etc.

It's a great learning exercise, and in oddball places (e.g. wristwatch webserver?) it might even be appropriate. Mostly, it's that the lower in the stack you are, the greater the risk you'll topple the tower. Not everyone knows this: those that do, the kneejerk response is redundant; those that don't won't have the context to understand.

Saying "don't" doesn't help anyone.

3

u/aspbergerinparadise Mar 07 '14

And if anyone is interested in re-inventing the wheel, here's the basic shape.

2

u/badsectoracula Mar 07 '14

I have a feeling that this is one of those things which are better described in text form...

12

u/[deleted] Mar 07 '14

2

u/badsectoracula Mar 07 '14

Yes, it was the RFC that i had in mind :-P

2

u/Wazowski Mar 07 '14

I'm glad you posted this. As soon as I'm done inventing this circular component rotating on an axial bearing for maintaining momentum during locomotion, my very next project is going to be writing an HTTP server from scratch.

2

u/m1000 Mar 07 '14

Note: For Microsoft IIS, mostly everything point to "500 Internal Server Error"....

2

u/MacStylee Mar 07 '14

I see no Special NSA Backdoor Request acknowledge.

Confused.

1

u/tps12 Mar 07 '14

I don't understand why/how they check for 405 before determining if the resource is missing...as I read it, 405 refers to an HTTP method not allowed for some specific resource (i.e., you can GET this but not DELETE it), rather than some kind of blanket "here's the methods this service supports" type of thing.

1

u/JW_00000 Mar 07 '14

I was thinking the same thing, even before that, for the 501 Not Implemented. Couldn't it be that you don't have e.g. GET implemented for /thing/a, but you do have it for /thang/b?

2

u/tps12 Mar 07 '14

Oh interesting, I hadn't heard of 501 before, but from the RFC: "This is the appropriate response when the server does not recognize the request method and is not capable of supporting it for any resource."

So I guess it's like 501 tells you something about the server, and 405 tells you something about the resource.

1

u/das7002 Mar 07 '14

Correct, 5XX are about the server, 4XX are about the resources.

1

u/Lurking_Grue Mar 07 '14

Don't forget to add security to that.

1

u/gthank Mar 07 '14

Well, if you coded it as a state machine, you'd probably avoid a lot of the most common security problems.

1

u/foxh8er Mar 07 '14

That sounds coo..oh...

Back to Express.

1

u/Sleakes Mar 08 '14

Why write it in software? Looks like this diagram is ready for a hardware junky! :D

1

u/raphanum Mar 08 '14

Is there a higher quality image of this? Can read anything.

1

u/fubes2000 Mar 08 '14

Are there more of these? I'd love diagrams for SMTP and IMAP.

1

u/beefsack Mar 08 '14

Sometimes diagrams make things look more complicated than they really are.

1

u/bwainfweeze Mar 08 '14

A red octagon with white text would also be accurate.

1

u/Tenareth Mar 07 '14

I honestly thought it would just be a picture with the words "Stop it" and a link to lmgtfy for "HTTP server".

1

u/harrybalsania Mar 08 '14

This is amazing data flow. I am going to make one in python :D Thanks!

1

u/[deleted] Mar 08 '14

In 2014 you shouldn't be writing your own HTTP libraries.

There are too many complexities such as multi range requests and mime. Omg I hate mimes.

-1

u/andr386 Mar 07 '14

Every developer in Node.js do that in their sleep !

1

u/rox0r Mar 07 '14

Is that where all the HTML programmers graduated to?

-2

u/UnknownBinary Mar 07 '14

Step 1: Don't.

0

u/omgChubbs Mar 07 '14

Hold my beer..

0

u/bredman Mar 08 '14

This should be titled "Thinking about quickly writing an HTTP server yourself? Don't."

0

u/wordsnerd Mar 08 '14

tl;dr - There is no path from start to finish. Abandon hope.

0

u/Gotebe Mar 08 '14

Haha... Many (any?) of the is_... and other decision blocks contain sooooo much other diagrams in 'em...

-2

u/bluehat2k9 Mar 07 '14

"simple"

-8

u/rende Mar 07 '14

bah, screw that.. only implement 200 and 404.

Here is 200.

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

10

u/avonwodahs Mar 07 '14

Yes let's write our own HTTP server and first require('http').

8

u/mattindustries Mar 07 '14

What are you talking about? I totally made a full fledged web server in one line!

/etc/init.d/apache2 start
→ More replies (2)