Skip to main content
Home Forums 68kMLA Help with TCPExample in Pascal — #15
Post #15 by mactjaap
Source Forum68kMLA
CategoryDevelopment
Post DateSun, 27 Oct 2013 - 17:48
Original URLhttps://68kmla.org/bb/threads/help-with-tcpexample-in-pascal.29110/
Post
Today I rounded up my experiment to build a small web server. It is working; returning a valid header and a small html page. All hardcoded in the app.

Not very usefull .... but a very nice way to see how one could develop a web server in the early '90s. This code is from around 1992 and that is the same era web servers began do exits.

See some information on Wikipedia about this:

A NeXT Computer was used by Berners-Lee as the world's first web server and also to write the first web browser, WorldWideWeb, in 1990. By Christmas 1990, Berners-Lee had built all the tools necessary for a working Web: the first web browser (which was a web editor as well); the first web server; and the first web pages, which described the project itself.
.............

On 6 August 1991, Berners-Lee posted a short summary of the World Wide Web project on the alt.hypertext newsgroup. This date also marked the debut of the Web as a publicly available service on the Internet, although new users only access it after August 23. For this reason this is considered the internaut's day. Many newsmedia have reported that the first photo on the web was uploaded by Berners-Lee in 1992, an image of the CERN house band Les Horribles Cernettes taken by Silvano de Gennaro;
To add more headers and more html code I had to add some more variables and TCPSend routines.

mactjaaphttpd-v1.0.JPG

Here is my complete code:

Code:
program MacTjaapHTTPD10;

{ From Peter's PNL Libraries }
{ Copyright 1992 Peter N Lewis }
{ This source may be used for any non-commercial purposes as long as I get a mention }
{ in the About box and Docs of any derivative program.  It may not be used in any commercial }
{ application without my permission }

uses
 TCPTypes, TCPStuff, TCPConnections;



const
 header = 'HTTP/1.1 200 OK';
 headerserver = 'Server: MacTjaapHTTPD/0.1';
 headerdate = 'Date: Fri, 25 Oct 2013 23:54:00';
 headerconnection = 'Connection: close';
 headerlast = 'Last-Modified: Fri, 20 Apr 1962 01:30:00';
 headercontent = 'Content-Type: text/html';

 html = 'This is the welcome page of a MacTjaapHTTPD server for Macintosh.
';
 html2 = 'It is based on TCPExample from Peter N Lewis, copyright 1992

';
 html3 = '

On this page is not much, but I will provide a link to the post in the 68kMLA forum:
';
 html4 = 'click here';

 dest_name = 'www.apache.org';
 nul = chr(0);
 lf = chr(10);
 cr = chr(13);
 localport = 80;
 remotehost = 0;
 remoteport = 0;
 dataptr = 0;



var
 oe: OSErr;
 cp: connectionIndex;
 quitNow: boolean;
 cer: connectionEventRecord; { Event record for TCP events, simmilar to EventRecord }
 s: str255;
 t: str255;
 u: str255;
 v: str255;

 count: longInt;
 gotlinefeed: boolean;
begin
ShowText;
quitNow := false;
oe := InitConnections; { Startup the TCP units }
if oe = noErr then begin
 oe := FindAddress(cp, dest_name, nil);
 if oe = noErr then begin
  count := 0;
  while not quitNow do begin
   if GetConnectionEvent(any_connection, cer) then begin { Get the next TCP event }
    case cer.event of
     C_Found:  begin






{function NewPassiveConnection (var cp: connectionIndex; buffersize: longInt; localport: integer; remotehost: longInt; remoteport: integer; dataptr: univ ptr): OSErr;}

      oe := NewPassiveConnection(cp, Default_TCPBUFFERSIZE, localport, remotehost, remoteport, nil);





     end;
     C_SearchFailed:  begin
      writeln('Couldn''t fine the address for ', dest_name);
      quitNow := true;
     end;
     C_Established:  begin { Happens once per succesful connection establishment }
      writeln('Connection Established');

      s := concat(header, cr, lf, headerserver, cr, lf, headerdate, cr, lf, headerlast, cr, lf, headercontent, cr, lf, cr, lf, html, cr, lf);
      t := concat(html2, cr, lf);
      u := concat(html3, cr, lf);
      v := concat(html4, cr, lf);

      oe := TCPSend(cer.tcpc, @s[1], length(s), true);
      oe := TCPSend(cer.tcpc, @t[1], length(t), true);
      oe := TCPSend(cer.tcpc, @u[1], length(u), true);
      oe := TCPSend(cer.tcpc, @v[1], length(v), true);

                           {to let the browser know that the page is sendI close the connection}
                           {function TCPClose (connection: TCPConnectionPtr; userptr: OSErrPtr): OSErr;}
      CloseConnection(cer.connection); { Close our side of the connection }



      if oe <> noErr then
      CloseConnection(cp); { Better close the connection if we can't send anything to it! }
     end;
     C_FailedToOpen: 
      writeln('Ooops, connection failed to open... Error is ', cer.value, ' Timed out is ', cer.timedout);
{ Example, network unreachable etc.  Error code is in cer.value }
     C_Closing:  begin
      writeln('Connection closing');{ Gets called when the connection starts closing down}
      CloseConnection(cer.connection); { Close our side of the connection }
     end;
     C_Closed:  begin
      writeln('Connection closed, quit now');
      quitNow := true; { The connection is closed, quit the program }
     end;
     C_CharsAvailable:  begin
{$PUSH}
{$R-}
      oe := TCPReceiveUpTo(cer.tcpc, 10, 60, @s[1], 255, count, gotlinefeed);
{ Recieve characters up to a line feed }
      if (count > 0) & (s[count] = lf) then { strip off linefeed }
      count := count - 1;
      if (count > 0) & (s[count] = cr) then { strip off cr }
      count := count - 1;
      s[0] := chr(count);
{$POP}
      if gotlinefeed then begin { if we got a linefeed, print the string, otherwise go round again and wait for more characters }
      writeln(s);
      count := 0;
      end;
     end;
    end;
   end;
  end;
 end;
 FinishEverything; { Close everything, clean up }
{ ALWAY CALL THIS, OR YOU WILL BE SORRY! }
end;
writeln('Click to quit');
while not Button do
 ;
end.
I will also add this latest version of the MacTjaapHTTPD server version 1.0.

MacTjaapHTTPD-V1.0.zip

For now I want to try to try some more:

- getting it working on System 6. Although System 7 was normal for 1992, System 6 has also MacTCP. So I hope it will run. For now the application starts, but immediately go to "Click to quit". Also the TCPExample App will do the same. My guess it doesn't call MacTCP in a right way. I have no clue what to about this so help is again appreciated!

- Reopen a listener after a connection is done. Now the application asked for "Click to quit" and can only handle one connection.... That is not what I want.... I tried a goto routine, but couldn't get it working....

- Maybe make it a real application. With a menu, about info, etc. Maybe that is possible in Turbo Pascal I use now. I will find out. It is the first Macintosh App I have made... so much to learn!

I specially want to thank bbraun for help with the building of this app!

mp.ls