6 ms·
Show HN: bashttp - a very (very) simple web server in Bash
- infamousjoeg 4y agoLove this minimalist approach!
- EddySchauHai 4y agoThanks! :)
- moondev 4y agonetcat doing all the work while bash gets all the glory
- whateveracct 4y agoThat's bash though - all the work is other programs. But the programmer makes it happen with bash.
- sshd 4y ago+1 on this. Would love to see this using bash built-ins if possible. /dev/tcp might be a start.
- BenjiWiebe 4y agoI believe the issue is that bash has no built-in method for creating a listening socket / accepting connections. /dev/tcp can only connect to a listening socket, not create one.
- rascul 4y agoThere's a loadable for that. http://git.savannah.gnu.org/cgit/bash.git/tree/examples/loadables/accept.c http://git.savannah.gnu.org/cgit/bash.git/tree/examples/load...
- arjvik 4y agoThat only works for outgoing connections, sadly
- EddySchauHai 4y agoI definitely want to try work this out but I couldn't find much in the way of documentation, I'll give it a go when I have more time :)
- tzury 4y agoMost likely inspired by https://en.wikipedia.org/wiki/Netcat#Performing_an_HTTP_request https://en.wikipedia.org/wiki/Netcat#Performing_an_HTTP_requ... ref: https://news.ycombinator.com/item?id=1873386 https://news.ycombinator.com/item?id=1873386
- EddySchauHai 4y agoVery much so! I wanted to write it in pure bash without nc but couldn't find a way straight away. The main change here is the -c which lets you drop the connection at EOF so you can have psudo-concurrent connections.
- rascul 4y agoThis is a wrapper around nc. Here's a web server in pure bash: https://github.com/dzove855/Bash-web-server https://github.com/dzove855/Bash-web-server
- sshd 4y agoI think this is the closest I've seen to a native bash implementation but the plugin is ... bending the meaning of native bash. Still very awesome
- rascul 4y agoThe plugin is for older bash. 5.2 includes it. It's built by default, but opensuse puts it in the bash-loadables package. Edit: I'm not sure what other distros might do with it.
- yakubin 4y agoWhat does ":" do in this? : "${BASH_LOADABLE_PATH:=/usr/lib/bash}"
- dzove855 4y agoCreator here of this server. : = does nothing but the := in the variable declariation will assign set the value of BASH_LOADABLE_PATH to /usr/lib/bash if the variable is empty. So instead of writing: BASH_LOADABLE_PATH=${BASH_LOADABLE_PATH:-/usr/lib/bash} you can just do : : "${BASH_LOADABLE_PATH:=/usr/lib/bash}"
- Brian_K_White 4y agoAnd the initial : is there to create a valid executable line where variables and brace expansions are actually evaluated, but without actually doing anything else. : is a synonym for true. Sort of like a comment in that no statement is executed (well a statement is executed, it's just a almost-no-op statement) but unlike a comment in that the braces are evaluated, and any actions they perform like assignments, are evaluated and performed. Most brace expansions just expand something in place, they essentially mostly just read and display something, possibly modifying it along the way from memory to output. But a few brace expansions actually do assignments. And that "set to this value if not already set to anything" is very useful. So a common trick is things like :${DEBUG:=false} : is just a synonym for "true" ie a valid command that does nothing but also does not produce an error and also is an executable statement not a comment. Everything after the true is evaluated for real just like if true were instead echo or any other command. The end result of that debug example is now all through the rest of the script you can put $DEBUG && echo doing step 7... and never have to worry about the invalid syntax condition if DEBUG had not been set to something. All else being equal, it's always going to be set to false, and so all those debug commands will be valid syntax. But you can enable verbose mode later at run-time without editing the script by running DEBUG=true myscript The := brace expansion means that since DEBUG was set to something, it's not modified, not set to false. it's only set to false if it was unset to begin with. It's basically the shortest way to define a configurable with a default value at the same time.
- tyingq 4y agoA terrible abomination with gawk: https://news.ycombinator.com/item?id=22085459 https://news.ycombinator.com/item?id=22085459
- nivethan 4y agoI wrote a more complex web server with sessions using bash (really nc is the core) https://github.com/Krowemoh/bash-server https://github.com/Krowemoh/bash-server
- EddySchauHai 4y agoThat’s really cool! I’m trying to work out how to do it without nc, did you find any good leads when you built this?
- dzove855 4y agoLike this you can do it without nc https://github.com/dzove855/Bash-web-server https://github.com/dzove855/Bash-web-server
- EddySchauHai 4y agoThanks! Great job on this btw :)
- Brian_K_White 4y agoUnneccessary $() fork, unnecessary cat, and at best a bit thoughtless to expand the file's contents onto a commandline just to squirt it. Sorry buddy, but, do not be proud of this. [edit I should not simply say that without showing what is supposedly the better way] no large cmdline, allow cat while : ;do { printf 'HTTP/1.1 200 OK\n\n' ;cat index.html ; } | nc -N -l $PORT ;done no cat, allow large cmdline while : ;do IFS= read -d '' -r x < index.html ;printf 'HTTP/1.1 200 OK\n\n%s' "$x" | nc -N -l $PORT ;done no cat, no large cmdline (though not strictly enforced) while : ;do { printf 'HTTP/1.1 200 OK\n\n' ;while read -r x ;do printf '%s' "$x" ;done <index.html ; } | nc -N -l $PORT ;done There are several versions of nc, mine uses -N to exit on eof. From here it's only a little more to read -n 4096 to read in 4k chunks instead of in lines, since a valid html document could actually be all on one line of arbitrary unlimited length, and the final version above would be no different from the "allow large cmdline" version.
- gammalost 4y ago> Sorry buddy, but, do not be proud of this A bit patronizing. It's just a fun and simple project. Not something to be all serious about
- chalst 4y ago> Sorry buddy, but, do not be proud of this. This is 'Show HN', not code review. You are being excessively negative.
- kubafu 4y agoThe tone of your comment is not nice. And if you went all the way to suggest improvements, you could have sent a PR already.
- tux2bsd 4y ago> Sorry buddy, but, do not be proud of this. You are a plonker.
- trhoad 4y ago
- raffraffraff 4y agoWhat about a systemd socket activated service that passes the network data through stdin? They'll spawn a new bash process for every incoming connection, and technically wouldn't need nc.
- EddySchauHai 4y agoInteresting! I’ll try that - my goal was to write it entirely in bash (without nc) but I really couldn’t find a quick solution
- yakshaving_jgt 4y agoA very (very) simple Hello World program in Haskell. {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE QuasiQuotes #-} {-# LANGUAGE TemplateHaskell #-} module Main where import Foreign.C.Types import qualified Language.C.Inline as C C.include "<stdio.h>" C.include "<stdlib.h>" main :: IO () main = [C.block| void { system("python -c 'print \"Hello, World!\"'"); } |]
- tpoacher 4y agolol, oh you!
- kretaceous 4y agoCan anyone explain how is this related to the post? I genuinely don't get it. I see C and Python/some kind of FFI.
- yakkomajuri 4y agoThe point here is that OP's "simple" Bash server just offloads everything to netcat. Just as the program above does with Python. EDIT: Just explaining the criticism, not necessarily subscribing to it - I don't think OP's point was to write something in "pure" Bash
- kretaceous 4y agoI see, thanks!
- M4tze 4y agoBetter tell him/her what you are thinking instead of making fun of him/her.
- dqft 4y agoBetter to just laugh at the good joke.
- 4y ago
- harvie 4y agohttps://github.com/Harvie/Programs/tree/master/bash/bash-httpd https://github.com/Harvie/Programs/tree/master/bash/bash-htt... with 30 lines of code it has directory listing and even tried to have something called BHP which is like PHP but bash.
- hestefisk 4y agoCould be done in one line of Perl.
- fio_ini 4y agoLove it. How can you do server side rendering? And does this handle errors?
- xwdv 4y agoSeems like the practical use for simple web servers like this is for hacking related activity.
- deleted 4y ago[deleted]
- masukomi 4y agosee also shinatra: https://github.com/benrady/shinatra https://github.com/benrady/shinatra > Shinatra - A web server in five lines of bash