Recent

Author Topic: Using pascal programs as backend for PHP based webpages?  (Read 5689 times)

mr-highball

  • Full Member
  • ***
  • Posts: 206
    • Highball Github
Re: Using pascal programs as backend for PHP based webpages?
« Reply #30 on: December 17, 2019, 03:59:43 pm »
I wrote some microservices that handled requests to/from a sqlite db a little while back, and it used the standalone web framework that comes with fpc (which makes it incredibly easy to deploy).
Here's the repos,
https://github.com/mr-highball/dcl-hackathon-2019 (initial repo with some implemented services)
https://github.com/mr-highball/dcl-microservices (trimmed up repo with only core stuff)

This is an example web service which was successfully built/deployed on a linux droplet running with digital ocean (so cross compiling to pi should work no prob). It takes image url's and dithers them down to a reduced color space, and offers endpoints to fetch the result from a sqlite db.
https://github.com/mr-highball/dcl-hackathon-2019/tree/master/services/image-service

here's some examples of an endpoint being used to return some content to the user,
https://github.com/mr-highball/dcl-hackathon-2019/blob/master/services/image-service/src/controller.status.pas#L146
https://github.com/mr-highball/dcl-hackathon-2019/blob/master/services/image-service/src/controller.registration.pas#L549

some niceties that you get if you use the stuff I wrote would be the automatic execution/serialization of db calls that contain result sets,
https://github.com/mr-highball/dcl-hackathon-2019/blob/master/services/image-service/src/controller.status.pas#L253

While the use case is not probably close to what you're doing, the code may help point you in the right direction.
By no means is this the only way to do things, just hoping it may help  :)


« Last Edit: December 17, 2019, 04:02:57 pm by mr-highball »

BosseB

  • Sr. Member
  • ****
  • Posts: 266
Re: Using pascal programs as backend for PHP based webpages?
« Reply #31 on: December 17, 2019, 07:55:05 pm »
I am sorry to have to come back here after I thought I had it all fixed....

I have created a getwebpage program in FPC and what it does is that it will output a webpage built internally by writing it out STDOUT when executed.
The content depends on how it is called:
- if the env variable QUERY_STRING is present it will read that and parse it (case GET)
- if there is a command line argument it will read this and parse it (case POST)
- if there is no data of any of the two kinds it will output an error page.

In the two first cases the output is a web page source with preamble, head with style section, body and the end tags. Preamble is the Mime header "Content-type: text/html"
Body is the decoded data from the input.
I have verified the creation of the webpage by setting up the env variable with the type of data from a GET operation and when I run this from the command line and send the output into a file it contains the exact expected content.

But when I call this from a web browser via Apache CGI handling I am always getting error 500 and the Apache error log says this:

Code: [Select]
[Tue Dec 17 19:18:04.837217 2019] [cgi:error] [pid 24911] [client 192.168.119.166:58361] malformed header from script 'getwebpage': Bad header: <!DOCTYPE html public >
The data sent out from the program looks like this:
Code: [Select]
Content-type: text/html

<!DOCTYPE html public >

<html>
<head>
<title>Monitoring Config</title>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
... long style section, then:
table, td, th {
  border: 1px solid black;
}
</style>
</head>

<body>
<p>Got Data:<br>namn=Olle<br>trafik=hemsk<br>Test=7</p>
</body></html>

I have googled extensively for the error and what I have seen and included without positive result (the error persists) is:
- I must include the Mime header as the first output followed by a blank line (done)
- I must include a blank line after the </head> line (done)

I have also looked at web pages that work normally around the web and checked their sources only to find the same kind of data I have put in the start of the file...

Why am I not able to get Apache to accept the page I am sending from my FPC program?
--
Bo Berglund
Sweden

lucamar

  • Hero Member
  • *****
  • Posts: 3028
Re: Using pascal programs as backend for PHP based webpages?
« Reply #32 on: December 17, 2019, 08:20:02 pm »
You should be sending out quite a lot more headers than just Content-Type (for example, Content-Lenght, IIRC). Read carefully the HTTP RFCs to see which headers are mandatory andhow to build a well-structured response.

Or better yet: use a CGI/HTML library which will do most of that for you automagically, even if just fcl-web, creating a "CGI application" in Lazarus.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.8/FPC 3.0.4 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

MarkMLl

  • Hero Member
  • *****
  • Posts: 1242
Re: Using pascal programs as backend for PHP based webpages?
« Reply #33 on: December 17, 2019, 08:21:58 pm »
Stop making life difficult for yourself, and start off with a /minimal/ page: cut out the metas, style and so on.

You need

Code: [Select]
Content-type: text/html; charset=ISO-8859-1

followed by two newlines to give yourself a blank line. Then something like

Code: [Select]
<html>
  <head>
  </head>
  <body>
  </body>
</html>

I'm not entirely sure about this, but assume for the moment that Apache is a unix program that it wants the unix line-end convention. The pages I created a couple of weeks ago, which I'm using with Apache and Perl, certainly use \n rather than \r\n

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.

MarkMLl

  • Hero Member
  • *****
  • Posts: 1242
Re: Using pascal programs as backend for PHP based webpages?
« Reply #34 on: December 17, 2019, 08:29:45 pm »
You should be sending out quite a lot more headers than just Content-Type (for example, Content-Lenght, IIRC).

Most of which will be generated by Apache. The only one that /has/ to be generated by a CGI script is Content-Type, and I suspect that many browers will make an interim assumption of HTML at least until they've seen whether there's a !DOCTYPE and whether the document markup language (some derivative of SGML) has anything useful in the headers.

So the only thing that /really/ has to be output is a blank line, and any extraneous characters can foul that up.

MarkMLl.
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.

dsiders

  • Sr. Member
  • ****
  • Posts: 376
Re: Using pascal programs as backend for PHP based webpages?
« Reply #35 on: December 17, 2019, 08:55:13 pm »
But when I call this from a web browser via Apache CGI handling I am always getting error 500 and the Apache error log says this:

Code: [Select]
[Tue Dec 17 19:18:04.837217 2019] [cgi:error] [pid 24911] [client 192.168.119.166:58361] malformed header from script 'getwebpage': Bad header: <!DOCTYPE html public >
The data sent out from the program looks like this:
Code: [Select]
Content-type: text/html

<!DOCTYPE html public >

<html>
<head>
<title>Monitoring Config</title>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
... long style section, then:
table, td, th {
  border: 1px solid black;
}
</style>
</head>

<body>
<p>Got Data:<br>namn=Olle<br>trafik=hemsk<br>Test=7</p>
</body></html>


Using a valid DOCTYPE would probably help:

HTML 5
Code: XML  [Select][+][-]
  1. <!DOCTYPE html>

HTML 4.01 Strict
Code: XML  [Select][+][-]
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

HTML 4.01 Transitional
Code: XML  [Select][+][-]
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

HTML 4.01 Frameset
Code: XML  [Select][+][-]
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">

XHTML 1.0 Strict
Code: XML  [Select][+][-]
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

XHTML 1.0 Transitional
Code: XML  [Select][+][-]
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2.  

XHTML 1.0 Frameset
Code: XML  [Select][+][-]
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">

XHTML 1.1
Code: XML  [Select][+][-]
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

You should also make sure the content-type in the Apache HEAD matches the UTF-8 content-type used in your HTML. You can modify the Apache config using:

Code: Text  [Select][+][-]
  1. AddDefaultCharset utf-8
or
Code: Text  [Select][+][-]
  1. AddCharset utf-8 .htm .html .js .css

Hope that helps...
« Last Edit: December 17, 2019, 09:02:37 pm by dsiders »
Lazarus 2.1 (SVN) / FPC 3.0.4 / FPC 3.2.0 / x86-win64 / Windows 8.1

bee

  • Sr. Member
  • ****
  • Posts: 393
Re: Using pascal programs as backend for PHP based webpages?
« Reply #36 on: December 18, 2019, 03:26:01 am »
The most minimalist code for CGI is as simple as this:

Code: Pascal  [Select][+][-]
  1. program test;
  2.  
  3. begin
  4.   writeln('content-type: text/html;');
  5.   writeln;
  6.   writeln('Hello world!');
  7. end.

Compiled with FPC 3.0.4. It works perfectly fine on my Ubuntu + Apache server here:
https://pak.lebah.web.id/test.cgi
-Bee-

A long time pascal lover.

BosseB

  • Sr. Member
  • ****
  • Posts: 266
Re: Using pascal programs as backend for PHP based webpages?
« Reply #37 on: December 18, 2019, 08:27:39 am »
I have got it working now but I sure do not know why it does (or why it didn't before)....

What I did is that I stripped down the generated html to bare minimums with just a few lines, no head etc. Then it worked.
Then I added back part by part and it still worked so finally I had the exact same page as before minus the line
<!DOCTYPE html public >
Still working so I added that too in and it still works!
So now I am back to square one with the same output but now it is accepted by Apache and sent back to the client....

Concerning the various doctype lines I have tried several of them and the last was cut from the source of a working webpage on the net. All earlier gave Apache hiccups and errors, but now not. I don't get it...
Now using:
Code: Pascal  [Select][+][-]
  1. <!DOCTYPE html>

There is only one item I did between non-working and working and this is trying to add fpWeb to Lazarus on suggestion from one contributor. Since Lazarus refused to rebuild the IDE whatever I did I went into the source dir and did:
Code: Pascal  [Select][+][-]
  1. make clean
  2. make bigide OPT=-dFPC_ARMHF FPC=/home/pi/bin/ppcarm
Then I had a fresh Lazarus and could rebuild the IDE with my own selection of extra packages plus fpWeb.
Next thing I did was to test a super-simple example, which worked. And finally went back as described above and it all worked.
Go figure.....
« Last Edit: December 18, 2019, 08:30:59 am by BosseB »
--
Bo Berglund
Sweden

MarkMLl

  • Hero Member
  • *****
  • Posts: 1242
Re: Using pascal programs as backend for PHP based webpages?
« Reply #38 on: December 18, 2019, 09:12:29 am »
You probably screwed a line ending around that special blank line. Remember that on Linux if you pipe a program's output through xxd you see the hex.

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.

bee

  • Sr. Member
  • ****
  • Posts: 393
Re: Using pascal programs as backend for PHP based webpages?
« Reply #39 on: December 18, 2019, 02:06:01 pm »
I have got it working now but I sure do not know why it does (or why it didn't before)....
Glad to read you made it worked.

Then I had a fresh Lazarus and could rebuild the IDE with my own selection of extra packages plus fpWeb.
This is my personal opinion, take it with extra grain of salt. I think fpWeb is great and all. But for simple web apps, it's overkill. I've made lots of simple web apps with 1-5 forms to serve less than 100 users. For that purpose, a simple and straight-forward unit like webCRT or webUtils is enough. The executable file size is a lot smaller too which is pretty important for CGI app (faster loading).

Unless you need to make a huge and complex web apps, fpWeb is more appropriate. It provides you with everything you need. It also scales very well. It's even better if you made your fpWeb app as FastCGI that responses instantly to Apache requests so it could handle lots more loads. It's like the desktop-class framework for web app, while unit like webCRT or webUtils is like a simple library for console or command line program in the DOS era.

Well, your mile may vary of course. Just share a thought of mind. :)
-Bee-

A long time pascal lover.

BosseB

  • Sr. Member
  • ****
  • Posts: 266
Re: Using pascal programs as backend for PHP based webpages?
« Reply #40 on: December 18, 2019, 05:05:47 pm »
This is my personal opinion, take it with extra grain of salt. I think fpWeb is great and all. But for simple web apps, it's overkill. I've made lots of simple web apps with 1-5 forms to serve less than 100 users. For that purpose, a simple and straight-forward unit like webCRT or webUtils is enough. The executable file size is a lot smaller too which is pretty important for CGI app (faster loading).
My current usage is for exactly 1 user...  ;D
To configure the RaspberryPi system before deployment and for navigating recorded data. All inside an SQLite database.
Speed is not a real issue.

Where do I find the webCRT and webUtils units?
EDIT:
Sorry, I found them both as links to GitHub in reply #14 on this thread by you....

EDIT2:
Now I have downloaded the two units from GitHub and also translated webUtils comments to English from Malayan
Using Google Translate, of course. It is pretty good at this...

Quote
Unless you need to make a huge and complex web apps, fpWeb is more appropriate. It provides you with everything you need. It also scales very well. It's even better if you made your fpWeb app as FastCGI that responses instantly to Apache requests so it could handle lots more loads. It's like the desktop-class framework for web app, while unit like webCRT or webUtils is like a simple library for console or command line program in the DOS era.
I installed the fpWeb package but then my original design started working so I have not looked further into it.
What does it give me when the simple CL program now works OK?
From the wiki I found I can create a CGI application (which is what this is all about) but when I tested it all I see is a very sparse main application with an initialization section for a web oriented object...
Quote
Well, your mile may vary of course. Just share a thought of mind. :)
I have a very simple use case but I want to avoid all hardware like displays, keyboards and the like. That is why I zoomed in on using a web based system.
The RPi will be set up as a WiFi Access Point to which you can connect your phone or tablet and then use the web browser to contact the system for config and results browsing. The RPi will have no Internet connection so you are the only user on the system.
I will make sure there is an index file that redirects to the web app so the proper pages will be shown.
« Last Edit: December 18, 2019, 06:26:12 pm by BosseB »
--
Bo Berglund
Sweden

bee

  • Sr. Member
  • ****
  • Posts: 393
Re: Using pascal programs as backend for PHP based webpages?
« Reply #41 on: December 19, 2019, 02:11:54 am »
My current usage is for exactly 1 user...  ;D
To configure the RaspberryPi system before deployment and for navigating recorded data. All inside an SQLite database.
Speed is not a real issue.
Well… that's a perfect case for my webCRT or webUtils unit. I also have an SQLite tutorial. But again, it's in Bahasa Indonesia.

Now I have downloaded the two units from GitHub and also translated webUtils comments to English from Malayan
Using Google Translate, of course. It is pretty good at this…
It's actually Bahasa Indonesia, not Malayan. But it doesn't matter, for english speaker, both are pretty much the same anyway.

From the wiki I found I can create a CGI application (which is what this is all about) but when I tested it all I see is a very sparse main application with an initialization section for a web oriented object…
Yes, fpWeb has lots of boilerplate. It's too complex for a simple web app. That's why I made webCRT unit. If you want more nifty control over the protocol, you could use the webUtils unit.

I have a very simple use case but I want to avoid all hardware like displays, keyboards and the like. That is why I zoomed in on using a web based system.
Yes, that's the advantage of web application.
-Bee-

A long time pascal lover.

PaulRowntree

  • Full Member
  • ***
  • Posts: 132
    • Paul Rowntree
Re: Using pascal programs as backend for PHP based webpages?
« Reply #42 on: December 19, 2019, 04:19:35 am »
Along with the tutorial, I also made a WebCRT unit to demonstrate how easy it's to write web apps using Pascal and the unit. It's almost as easy as using the standard CRT unit.

For more control but with more "manual" way, you may use my webUtils unit, like to handle sessions and cookies. The repo contains the codes of my web app tutorial. I think you can learn something from them although you don't read the tutorial.

Hope it will help.
Thanks Bee!
Paul Rowntree
- coding for instrument control, data acquisition & analysis, CNC systems

 

TinyPortal © 2005-2018