LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to write a HTTP server using bash script (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-write-a-http-server-using-bash-script-644921/)

sawaby 05-26-2008 03:24 PM

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

acid_kewpie 05-26-2008 04:08 PM

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.

seraphim172 05-26-2008 04:55 PM

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

onebuck 05-26-2008 10:03 PM

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.

Electro 05-26-2008 10:25 PM

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.

sawaby 05-28-2008 03:10 PM

thanks i am trying to learn it from the code and sources,
of course im not cheating, but learning

kamathln 06-07-2008 02:07 PM

Do you want a "c-" ?
 
Quote:

Originally Posted by Electro (Post 3165535)
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.

Very much correct. The code is currently in it's ugliest state, and highly incomplete. It is the definition of bad code. So much of repeated code. No proper structure. No comments.

If you copy the code, remember that these steps are what you need to do next ( all of them, compulsory).
  1. Realize it is an ongoing hobby project.
  2. Realize what the code is doing.
  3. Abstract it. ( to reduce code-repeatation, and ecnourage re-use)
  4. Refactor to reflect the abstraction.
  5. Complete it.
  6. Write proper comments at appropriate places.
  7. Write Documentation.
  8. Post a reply here after you are done so we can all enjoy your code :-P

If you don't do the above, you may at max get "c-".

fbianconi 06-08-2008 04:06 AM

I would start by taking a look to nc, socat, and bash man pages.
Also RFC 1626 (HTTP/1.1).
Good luck

lwasserm 06-09-2008 10:31 AM

Here you go:

#!/bin/sh
/usr/bin/apache --your-options-here

Your Welcome!
:wq

sawaby 06-10-2008 06:14 PM

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*******************************

Baaqikhan 12-01-2011 03:28 PM

Quote:

Originally Posted by sawaby (Post 3180848)
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*******************************

can you say how to run? as you mentioned above, I could n't run the program, if it is possible please give details

Baaqikhan 12-01-2011 03:29 PM

there is some mistake as well, in line 18 throgh to 24, would u tell me what is wrong there?
Thanks

onebuck 12-01-2011 03:46 PM

Hi,

Welcome to LQ!

Quote:

Originally Posted by Baaqikhan (Post 4539495)
there is some mistake as well, in line 18 throgh to 24, would u tell me what is wrong there?
Thanks

You will probably not hear from the OP(Original Poster) since no posts or threads since 2008.

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!

Baaqikhan 12-02-2011 11:28 AM

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.