Hacker News new | past | comments | ask | show | jobs | submit login
Show HN: bashttp - a very (very) simple web server in Bash (github.com/eddyschauhai)
92 points by EddySchauHai on July 20, 2022 | hide | past | favorite | 43 comments



A 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!\"'");
      } |]


Can anyone explain how is this related to the post? I genuinely don't get it.

I see C and Python/some kind of FFI.


The 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


I see, thanks!


lol, oh you!


Better tell him/her what you are thinking instead of making fun of him/her.


They did. And in a perfectly valid way.

Better to not announce to the world "Hey everyone! Whaddya think?" If you can only tolerate approving answers. No one made them post a bad version of a one-liner that could already be googled up from 20 other examples already out there and better engineered.

This sort of send-up is actually perfectly on target, and frankly actaully light hearted.

Everyone posting critiques like this and like my own, knows full well that they have their own excrable works of art out there. We've all been figurative 7 year olds and made terrible crayon drawing programs, and even been ignorantly and adorably proud of them at the time.


Better to just laugh at the good joke.


Username checks out.


This is a wrapper around nc.

Here's a web server in pure bash:

https://github.com/dzove855/Bash-web-server


I 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


The 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.


What does ":" do in this?

  : "${BASH_LOADABLE_PATH:=/usr/lib/bash}"


Creator 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}"


And 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.


Unneccessary $() 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.


> 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


> Sorry buddy, but, do not be proud of this.

This is 'Show HN', not code review. You are being excessively negative.


The tone of your comment is not nice. And if you went all the way to suggest improvements, you could have sent a PR already.


> Sorry buddy, but, do not be proud of this.

You are a plonker.


netcat doing all the work while bash gets all the glory


That's bash though - all the work is other programs. But the programmer makes it happen with bash.


+1 on this. Would love to see this using bash built-ins if possible. /dev/tcp might be a start.


I 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.



I 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 :)


That only works for outgoing connections, sadly


A terrible abomination with gawk: https://news.ycombinator.com/item?id=22085459


What 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.


Interesting! I’ll try that - my goal was to write it entirely in bash (without nc) but I really couldn’t find a quick solution


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.



Very 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.


I wrote a more complex web server with sessions using bash (really nc is the core)

https://github.com/Krowemoh/bash-server


That’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?


Like this you can do it without nc https://github.com/dzove855/Bash-web-server


Thanks! Great job on this btw :)


see also shinatra: https://github.com/benrady/shinatra

> Shinatra - A web server in five lines of bash


Seems like the practical use for simple web servers like this is for hacking related activity.


Love this minimalist approach!


Thanks! :)


Love it. How can you do server side rendering? And does this handle errors?


Could be done in one line of Perl.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: