how to write a HTTP server using bash script
hi every one
((( i would like write an HTTP server as a bash script. The server should be called as follows: # ./bashwebserver.sh port [root] ● The web server should at least understand the HTTP GET command ● The web server should correctly return the following status codes: 200 OK, 403 FORBIDDEN, 404 NOT FOUND ● The web server should correctly return a Content-Type header, at least for text/plain, text/html and application/octet-stream (for all files which do not match any of the other content types) ● If the web server receives a request for a directory, it should return a directory listing . ))) but as i am new in writing bash scripting i don't know how to do it, any idea for solving this problem would be appreciated. thanks |
You seem to have already asked your homework question on daniweb, and have plenty of answers there. We aren't here to help you cheat at you work, sorry.
|
why a shell script?
Writing a web server is certainly possible as a shell script, but it's probably not the most reasonable environment to implement a web server.
Why has it to be a shell script? Even more, if you have no experience with bash shell scripting, then the first step has to be to learn at least the basics. For the requested items in your wishlist you wouldn't need much more then the echo and the ls commands with some proper formatting. Note: the HTTP header has to end with double linefeeds before sending content data. Linux Archive |
Hi,
If this is homework then you should post what you have attempted. We could then advice what you could or need to do. You need to place some effort in anything that you attempt to do. We are not here to do the work for you but to enhance your LQ experience by helping you to understand your weak points or errors. |
Even though you ask a homework like question. Some one already did write a a web server using BASH.
http://kamathln.homelinux.net/bashhttpd Do not copy the code. Write the code at a different angle. |
thanks i am trying to learn it from the code and sources,
of course im not cheating, but learning |
Do you want a "c-" ?
Quote:
If you copy the code, remember that these steps are what you need to do next ( all of them, compulsory).
If you don't do the above, you may at max get "c-". |
I would start by taking a look to nc, socat, and bash man pages.
Also RFC 1626 (HTTP/1.1). Good luck |
Here you go:
#!/bin/sh /usr/bin/apache --your-options-here Your Welcome! :wq |
bash web server
hi every one
finally i got a result for my question. i decided to share it , some one else may need it some time. here is a copy of the final result. hope it is interesting for you. :) how to use it? - you should run it first in the terminal - then listen on the port you specified in the browser -you can read a file in the browser listening to this port ******************************start***************************** #!/bin/bash PORT=$1 if [[ $2 == "" ]]; then root=. else root=$2 fi function call() { read REQUEST LOCATION PORT < receive if [[ -r ${root}/${LOCATION} ]]; then if [[ -d ${root}/${LOCATION} ]]; then cat plainheaders # it is a directory containing the file i read ls -l "${root}/${LOCATION}" else case "${LOCATION}" in *.txt) cat plainheaderfile # cat the file ;; *.html) cat htmlheaderfile # cat the file ;; *.png) cat pngheaderfile# cat the file ;; *) cat applicationosheaderfile # cat the file ;; esac cat "${root}/${LOCATION}" fi else if [[ -e ${root}/${LOCATION} ]]; then cat 403forbidden # cat the file else # not found, send error page cat 404notfound # cat the file fi fi } mkfifo receive mkfifo send echo listening on port $PORT while true; do cat send | netcat -l -p $PORT > receive & serve > send done echo "cleaning up" rm receive send ****************************end******************************* |
Quote:
|
there is some mistake as well, in line 18 throgh to 24, would u tell me what is wrong there?
Thanks |
Hi,
Welcome to LQ! Quote:
This is a 3 year old thread! What errors are you getting? Do you have the default header files that are referenced within the script? Not always a good practice to resurrect old threads for this very reason of no contact. Just a few links to aid you to gaining some understanding; 1 Linux Documentation Project 2 Rute Tutorial & Exposition 3 Linux Command Guide 4 Bash Beginners Guide 5 Bash Reference Manual 6 Advanced Bash-Scripting Guide 7 Linux Newbie Admin Guide 8 LinuxSelfHelp 9 Utimate Linux Newbie Guide 10 Linux Home Networking 11 Virtualization- Top 10 The above links and others can be found at 'Slackware-Links'. More than just SlackwareŽ links! |
Thanks for the links, hope I can get some point from these links.
|
All times are GMT -5. The time now is 07:23 PM. |