Discussion:
Question about httpd.fs
(too old to reply)
Philip Smith
2013-01-19 21:32:53 UTC
Permalink
Good day everybody! I have a raspberry pi with Occidentalis ver 0.2 image on it. This is Debian Wheezy flavor of Linux. I have installed gforth via apt-get ( $ sudo apt-get install gforth ) Wanted to do some GPIO stuff with some basic web page stuff and gforth scripting. So to that end i found Bernd Paysan's PDF on Web-Server in Forth. It took me some time to find the httpd.fs but i have a fork of the gforth stuff now on git hub. So i have the system set up now with out the proxy stuff in httpd.fs used and the inetd is configured to work on port 4444 just for testing.
The code works it seems but for the scripting part. This of course was the part i wanted to use so i need some help. My thoughts are why does the code run but not the scripting part? I have no idea but following are some clues (this is output of a simple index.html file):

###########################
Computing Primes
<$ 25 Constant #prim $>

The first <$ #prim $> primes are: <$ : prim? 0 over 2 max 2 ?DO over I mod 0= or LOOP nip 0= ; : prims ( n - ) 0 swap 2 swap 0 DO dup prim? IF swap IF ." , " THEN true swap dup 0 .r 1+ 1 ELSE 1+ 0 THEN +LOOP drop ; #prim prims $> .
###########################
This is the html file:

<HTML>
<HEAD>
<TITLE>GForth <$ version-string type $> presents</TITLE>
</HEAD>
<BODY>
<H1>Computing Primes</H1> <$ 25 Constant #prim $>
<P>The first <$ #prim $> primes are: <$
: prim? 0 over 2 max 2 ?DO over I mod 0= or LOOP nip 0= ;
: prims ( n - ) 0 swap 2
swap 0 DO dup prim? IF swap IF ." , " THEN true swap
dup 0 .r 1+ 1 ELSE 1+ 0 THEN
+LOOP drop ;
#prim prims $> .</P>
</BODY>
</HTML>
#####################################
So it is clear that the script part is just echoed out to a browser. The second clue is as follows:
From sshing into the raspberry pi and then running the httpd.fs file like this: $ ./httpd.fs index.html
I get this output:
<HTML>
<HEAD>
<TITLE>GForth 0.7.0 presents</TITLE>
</HEAD>
<BODY>
<H1>Computing Primes</H1>
<P>The first primes are: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97 .</P>
</BODY>
</HTML>

This output in the ssh if it was echoed to the browser would be a working scripting webserver. No i know executing the httpd.fs file this way in a ssh viewer is not what was intended and but you can see that this told me the scripting part was executing correctly so i am stumped as to why it seems to work in one context but not the other. I just wanted a basic starting point for a scripting forth webserver and if i can not use this httpd.fs then i will just make my own. Could someone possibly give me a link for another httpd.fs file that is know to work with gforth? Or possibly could someone identify what might be the issue with these two clues? It might not be obvious but in order for the httpd.fs file to be executable i needed to run this: $ chmod +x httpd.fs This is not clearly explained in anything i could read about how to use this web server but i believe i do understand that this is necessary because of the #! /usr/bin/gforth commands at the beginning of httpd.fs file.

Sorry for being long with this question but i figure this information might give someone an idea of what i need to do to solve this. I should also say that i have commented out the proxy stuff in httpd.fs because it does not compile correctly so i will need to look at that also but i figured i do not need a proxy yet. My start to raspberry pi gpio in gforth using c wrapper functions is here on git hub: https://github.com/harrypower/Rpi_Gforth_GPIO Now it does use c code i forked from another author but again i am trying to do this stuff with out reinventing the wheel. Also i am new to Linux so i am using this stuff as a learning platform. I do have a good history with C, Forth and several microcontroller family's so the Linux stuff is the new addition. If Bernd Paysan is around this group i would like to say Thanks for sharing what you have regarding forth and i have enjoyed Gforth plenty! I cut my teeth with Tom Zimmer flavor of Forth's all the way from Vic20 and C64 days to present in many forms including win32forth. Anyways i digress ... i hope someone can direct me further to allow me to do some web stuff with forth scriping so i can do some pi GPIO stuff! Thanks again!
Bernd Paysan
2013-01-19 22:39:01 UTC
Permalink
Post by Philip Smith
Good day everybody! I have a raspberry pi with Occidentalis ver
0.2 image on it. This is Debian Wheezy flavor of Linux. I have
installed gforth via apt-get ( $ sudo apt-get install gforth )
Wanted to do some GPIO stuff with some basic web page stuff and
gforth scripting. So to that end i found Bernd Paysan's PDF on
Web-Server in Forth. It took me some time to find the httpd.fs but
i have a fork of the gforth stuff now on git hub. So i have the
system set up now with out the proxy stuff in httpd.fs used and the
inetd is configured to work on port 4444 just for testing.
The code works it seems but for the scripting part.
If you name your script something.shtml, the scripting "engine" (those
four lines of code) will interpret it. The s is important, if the
suffix is just html, it's considered as a non-scripted file.
--
Bernd Paysan
"If you want it done right, you have to do it yourself"
http://bernd-paysan.de/
Philip Smith
2013-01-20 00:47:27 UTC
Permalink
Post by Bernd Paysan
If you name your script something.shtml, the scripting "engine" (those
four lines of code) will interpret it. The s is important, if the
suffix is just html, it's considered as a non-scripted file.
--
Bernd Paysan
"If you want it done right, you have to do it yourself"
http://bernd-paysan.de/
Hello Bernd! Thank you for your reply.
I clearly do not understand. I get the idea of .shtml for the "engine" to use file as script but is this engine other then gforth running via the script that is httpd.fs? You mention "(those four lines of code)" what are you referencing here?
Let me maybe back up .... I installed inetd via $ sudo apt-get install inetutils-inetd . This Debian version that i am using on the raspberry pi may not have other setup or other configured software to do this "engine" behavior that may be in other Linux systems. I believed the inetd simply, when the port is connected externaly, connects the standard input to the program you configure it so excecute. That program i thought was the script in httpd.fs. I then thought this script in turn starts gforth up and compiles and starts the code to interpret this standard input. I then thought the scripting happens because the <HTML> tag is found and this starts the $> word that then starts looking for the <$ string from input and simply cleans up the input and compiles and executes the script there that then may put other strings out that then is viewed on the browser. Clearly i am missing something here that i must learn.
What i did to test what i think you are saying is as follows:

index.html contains following:
<ul><li><a href="testing.shtml">Thing &ndash; place</a></li></ul>

testing.shtml contains following:
<HTML>
<HEAD>
<TITLE>GForth <$ version-string type $> presents</TITLE>
</HEAD>
<BODY>
<H1>Computing Primes</H1> <$ 25 Constant #prim $>
<P>The first <$ #prim $> primes are: <$
: prim? 0 over 2 max 2 ?DO over I mod 0= or LOOP nip 0= ;
: prims ( n - ) 0 swap 2
swap 0 DO dup prim? IF swap IF ." , " THEN true swap
dup 0 .r 1+ 1 ELSE 1+ 0 THEN
+LOOP drop ;
#prim prims $> .</P>
</BODY>
</HTML>

I know this is not what you were thinking Bernd but i thought i might get me somewhere. In fact it did not do anything different. I understand if i was running an apache web server that it would have a different behavior and would maybe act as you say correctly with the .shtml files.
I thank you for your help though i will clearly need more information. Sincerly yours from ---> searching for basic web server in forth with forth scripting.
It maybe as Charles Moore says "Do it yourself!" quote from PROGRAMMING A PROBLEM-ORIENTED-LANGUAGE. I will continue to try to understand how this httpd.fs is to work and how scripting is done with this code because i believe there could be a considerable time savings here!
Bernd Paysan
2013-01-20 14:09:33 UTC
Permalink
Post by Philip Smith
Post by Bernd Paysan
If you name your script something.shtml, the scripting "engine" (those
four lines of code) will interpret it. The s is important, if the
suffix is just html, it's considered as a non-scripted file.
--
Bernd Paysan
"If you want it done right, you have to do it yourself"
http://bernd-paysan.de/
Hello Bernd! Thank you for your reply.
I clearly do not understand. I get the idea of .shtml for the
"engine" to use file as script but is this engine other then gforth
running via the script that is httpd.fs? You mention "(those four
lines of code)" what are you referencing here?
Those:

: $> ( -- )
BEGIN source >in @ /string s" <$" search 0= WHILE
type cr refill 0= UNTIL EXIT THEN
nip source >in @ /string rot - dup 2 + >in +! type ;
: <HTML> ( -- ) ." <HTML>" $> ;

They are actually 5 lines ;-).
Post by Philip Smith
Let me maybe back up .... I installed inetd via $ sudo apt-get
install inetutils-inetd . This Debian version that i am using on
the raspberry pi may not have other setup or other configured
software to do this "engine" behavior that may be in other Linux
systems. I believed the inetd simply, when the port is connected
externaly, connects the standard input to the program you configure
it so excecute. That program i thought was the script in httpd.fs.
I then thought this script in turn starts gforth up and compiles
and starts the code to interpret this standard input. I then
thought the scripting happens because the <HTML> tag is found and
this starts the $> word that then starts looking for the <$ string
from input and simply cleans up the input and compiles and executes
the script there that then may put other strings out that then is
viewed on the browser.
Yes, that's what it does. The scripting happens if the file extension
is shtml, it doesn't script html files. The thing you are doing
definitely work - here on my local machine.
Post by Philip Smith
I know this is not what you were thinking Bernd but i thought i
might get me somewhere. In fact it did not do anything different.
Well, it does work here. Your thinking is correct.

In general, I would use telnet first for testing. There, you can also
see the server response. It should look somehow like that (the
GET+connection+empty line is what you enter):

% telnet localhost 4444
GET /testing.shtml HTTP/1.1
Connection: closed

HTTP/1.1 200 OK
Server: Gforth httpd/1.0 (unix)
Connection: close
Content-Type: text/html

<HTML>
<HEAD>
<TITLE>GForth 0.7.9-20121031 presents</TITLE>
</HEAD>
<BODY>
<H1>Computing Primes</H1>
<P>The first 25 primes are: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37,
41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97.</P>
</BODY>
</HTML>
--
Bernd Paysan
"If you want it done right, you have to do it yourself"
http://bernd-paysan.de/
Philip Smith
2013-01-21 17:59:20 UTC
Permalink
Post by Bernd Paysan
: $> ( -- )
type cr refill 0= UNTIL EXIT THEN
: <HTML> ( -- ) ." <HTML>" $> ;
They are actually 5 lines ;-).
Yes, that's what it does. The scripting happens if the file extension
is shtml, it doesn't script html files. The thing you are doing
definitely work - here on my local machine.
Well, it does work here. Your thinking is correct.
In general, I would use telnet first for testing. There, you can also
see the server response. It should look somehow like that (the
% telnet localhost 4444
GET /testing.shtml HTTP/1.1
Connection: closed
HTTP/1.1 200 OK
Server: Gforth httpd/1.0 (unix)
Connection: close
Content-Type: text/html
<HTML>
<HEAD>
<TITLE>GForth 0.7.9-20121031 presents</TITLE>
</HEAD>
<BODY>
<H1>Computing Primes</H1>
<P>The first 25 primes are: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37,
41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97.</P>
</BODY>
</HTML>
Bernd Paysan
Hello again Bernd! Thanks for your response. I appreciate the help that you have given to these questions of mine.
It would seem that still there is much more i need to learn. I have changed to the most current raspberry pi wheezy image and reinstalled inetd and transfered the files i was using to test and run this webserver example. I then setup /etc/inetd.conf like i had it before on the old image and also made addition to /etc/services. After all of this i got the same results!
Again to be clear the results are as follows:
-In firefox browser running on a windows machine that is on the same network as the raspberry pi i get the echoed out forth commands as in my first post above. If i look at the source i see the html with the echoed forth commands as first post above. If i use chrome browser i get the same results.
-Via putty on the same windows machine as first result i ssh into the raspberry pi and in the directory where httpd.fs is located i do $ ./httpd.fs testing.shtml i get the results as my first post where the html is echoed and the prime numbers are displayed. This is like your telnet example Bernd. The difference is i am using putty to ssh into my pi.

I believe i will just need to do some more research on this and some code testing stuff to try to understand what is happening. I will try to take apart httpd.fs to get some better understanding of it. Thank you very much for all your help Bernd. Once i get more information to report or some kind of resolve i will repost. Truly this is more my ignorance then anything else so i will maybe in the end just write my own version of this webserver! My ignorance of linux is also an issue here so this may be part of the issue so i will need to work through that also.

PKS
Philip Smith
2013-01-23 03:23:28 UTC
Permalink
Good day everybody!
Well i have solved the issue that i was having! The answer is as follows:
The primary answer was the file called /etc/mime.types in my Debian Wheezy Raspberry Pi distro had this line:
text/html html htm shtml
I changed this line to read as follows:
text/html html htm
I am unsure if this is the proper way to do this but i got to this solution because of the fact that testing showed that the command shtml in the httpd.fs file was never executing. I worked through this code for hours with wireshark and other tools like inserting text in the code of httpd.fs that should show up in the standard output. I could prove that the file type .shtml was correctly found but this seemed to never cause the shtml code to run. Then i realized that this /etc/mime.types file was possibly the reason so after i changed the line as above all issues went away.
Now if you were following this thread you may have wondered how did my second clue in above thread make me think that the script was running. I realized when going through the code that running the httpd.fs file like ./httpd.fs testing.shtml caused the testing.shtml file to be included and as the httpd.fs file created a command called <HTML> and that is the first thing that was in my testing.shtml file that then would start the scripting process properly. But this was a clue that i thought showed the scripting worked for standard input and that was not the case. That is why my first clue above would never execute the shtml command in httpd.fs because it entered the httpd.fs code via the inetd process then to httpd.fs code and back out to inetd.
Anyways i will use this basic web server with other changes that i need to do what i want with it now that it is working. Thank you again Bernd for making this available for me to find and use.
I still need to find out why this /etc/mime.types is made the way it is and if it should be used the way it is in httpd.fs but for my purposes it will work fine. If others want to use the scripting ability of this webserver and find it does not work i guess look at /etc/mime.types for the above mentioned line and change the line or come up with another solution. It would seem to me that an apache server may do this scripting process in another way so changing this /etc/mime.types may or may not cause problems on other hardware but on my raspberry pi i will have no issues!

PKS

Loading...