LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-26-2008, 04:24 PM   #1
sawaby
LQ Newbie
 
Registered: May 2008
Posts: 6

Rep: Reputation: 0
Unhappy 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
 
Old 05-26-2008, 05:08 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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.
 
1 members found this post helpful.
Old 05-26-2008, 05:55 PM   #3
seraphim172
Member
 
Registered: May 2008
Posts: 101

Rep: Reputation: 15
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

Last edited by seraphim172; 06-25-2008 at 05:27 AM.
 
Old 05-26-2008, 11:03 PM   #4
onebuck
Moderator
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: SlackwareŽ
Posts: 12,899
Blog Entries: 27

Rep: Reputation: 2186Reputation: 2186Reputation: 2186Reputation: 2186Reputation: 2186Reputation: 2186Reputation: 2186Reputation: 2186Reputation: 2186Reputation: 2186Reputation: 2186
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.
 
Old 05-26-2008, 11:25 PM   #5
Electro
LQ Guru
 
Registered: Jan 2002
Posts: 6,042

Rep: Reputation: Disabled
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.
 
Old 05-28-2008, 04:10 PM   #6
sawaby
LQ Newbie
 
Registered: May 2008
Posts: 6

Original Poster
Rep: Reputation: 0
thanks i am trying to learn it from the code and sources,
of course im not cheating, but learning
 
Old 06-07-2008, 03:07 PM   #7
kamathln
LQ Newbie
 
Registered: Jun 2003
Location: Mangalore,India
Distribution: redhat 8 (currently) but i keep changing distros
Posts: 5

Rep: Reputation: 0
Exclamation Do you want a "c-" ?

Quote:
Originally Posted by Electro View Post
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-".

Last edited by kamathln; 06-07-2008 at 03:09 PM.
 
Old 06-08-2008, 05:06 AM   #8
fbianconi
Member
 
Registered: Apr 2008
Location: argentina
Distribution: Arch
Posts: 86

Rep: Reputation: 22
I would start by taking a look to nc, socat, and bash man pages.
Also RFC 1626 (HTTP/1.1).
Good luck
 
Old 06-09-2008, 11:31 AM   #9
lwasserm
Member
 
Registered: Mar 2008
Location: Baltimore Md
Distribution: ubuntu
Posts: 184

Rep: Reputation: 41
Here you go:

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

Your Welcome!
:wq
 
Old 06-10-2008, 07:14 PM   #10
sawaby
LQ Newbie
 
Registered: May 2008
Posts: 6

Original Poster
Rep: Reputation: 0
Smile 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*******************************
 
Old 12-01-2011, 04:28 PM   #11
Baaqikhan
LQ Newbie
 
Registered: Dec 2011
Posts: 3

Rep: Reputation: Disabled
Quote:
Originally Posted by sawaby View Post
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
 
0 members found this post helpful.
Old 12-01-2011, 04:29 PM   #12
Baaqikhan
LQ Newbie
 
Registered: Dec 2011
Posts: 3

Rep: Reputation: Disabled
there is some mistake as well, in line 18 throgh to 24, would u tell me what is wrong there?
Thanks
 
0 members found this post helpful.
Old 12-01-2011, 04:46 PM   #13
onebuck
Moderator
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: SlackwareŽ
Posts: 12,899
Blog Entries: 27

Rep: Reputation: 2186Reputation: 2186Reputation: 2186Reputation: 2186Reputation: 2186Reputation: 2186Reputation: 2186Reputation: 2186Reputation: 2186Reputation: 2186Reputation: 2186
Hi,

Welcome to LQ!

Quote:
Originally Posted by Baaqikhan View Post
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!
 
Old 12-02-2011, 12:28 PM   #14
Baaqikhan
LQ Newbie
 
Registered: Dec 2011
Posts: 3

Rep: Reputation: Disabled
Thanks for the links, hope I can get some point from these links.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Trying to write a bash script and I'm stuck Yig Programming 12 04-16-2008 11:04 AM
Trying to write a bash script christianunix Linux - Newbie 5 10-26-2007 06:30 PM
read write in bash script yhus Programming 6 09-18-2006 01:23 PM
Need to write a bash script imagineers7 Linux - General 5 05-10-2006 12:17 AM
Anoyone willing to write a BASH script for me? pilot1 Programming 4 09-16-2003 09:56 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:08 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration