Using pascal programs as backend for PHP based webpages? lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- BosseB · 2019-12-15 17:12 I have built a Raspberry Pi based data collection system and I have used FreePascal and Lazarus to program its control software running on the RPi. This consists of a few console programs intended to be run by cron. The data are managed inside an SQLite database (for which I have received good help on this forum!) Now I want to build a configuration utility that will be web based (Apache2 on the RPi) and originally I thought that I would use PHP7 and straight HTML coding. But that would mean to somehow re-create the database and other functions I have already developed in Pascal so it would work with PHP... Not so fun. Why web based? Well, it makes it possible to use any computer with a web browser to configure the system when connected to the same network rather than logging on to the RPi itself, which is headless (no monitor or keyboard attached). Question: Is there some simple way to call processing functions written in Pascal from within PHP such that it can retrieve data to display to the user or execute commands and feed back results? All I really want is read and write to the SQLite database and also scan a few directories for certain files, list and down/upload these. Has someone here been there and done that and is willing to share the experience? ---------------------------------------------------------------------- PaulRowntree · 2019-12-15 17:22 Ignorant question coming ...Why do you need the PHP intermediate layer? ---------------------------------------------------------------------- MarkMLl · 2019-12-15 17:23 I'd suggest https://wiki.freepascal.org/CGI_Web_Programming and related pages. I've just set up a very simple webpage on a server run by our ISP which will eventually be a portal to various backend stuff- probably in VMs. They don't support PHP etc but do support CGI, and I wrote a simple script in Perl which expands SSIs in a page passed as the parameter and emits the result. MarkMLl ---------------------------------------------------------------------- bee · 2019-12-15 17:30 You don't need to use PHP to make web apps. Just use Pascal directly, either as CGI, FastCGI, or Apache module. Free Pascal provides fpWeb framework for everything you need to make web apps. I've made lots of web apps using FPC since like 15 years ago. I've never need PHP or nodeJS or any non-Pascal language for that. Now it's even better, Free Pascal has a sister project named pas2js that convert pascal program into javascript. So, you can even write the front-end side using pascal as well. Isn't that awesome? 😊 ---------------------------------------------------------------------- BosseB · 2019-12-15 17:58 Quote from: PaulRowntree on December 15, 2019, 06:22:43 pmIgnorant question coming ...Why do you need the PHP intermediate layer? Because that is the only way I (kind of) know how to use for programming stuff on webpages... There is a way to call an external executable on the webserver from PHP and somehow retrieve the result, I guess. Since I have written a number of related utilities in pascal and compiled for Raspbian they are available to be called. Only I don't really know how to extract the result from them... ---------------------------------------------------------------------- BosseB · 2019-12-15 18:00 Quote from: MarkMLl on December 15, 2019, 06:23:29 pmI'd suggest https://wiki.freepascal.org/CGI_Web_Programming and related pages. I've just set up a very simple webpage on a server run by our ISP which will eventually be a portal to various backend stuff- probably in VMs. They don't support PHP etc but do support CGI, and I wrote a simple script in Perl which expands SSIs in a page passed as the parameter and emits the result. MarkMLl Hi Mark, I will have a look at that documentation. Thanks. ---------------------------------------------------------------------- BosseB · 2019-12-15 18:03 Quote from: bee on December 15, 2019, 06:30:42 pmYou don't need to use PHP to make web apps. Just use Pascal directly, either as CGI, FastCGI, or Apache module. Free Pascal provides fpWeb framework for everything you need to make web apps. I've made lots of web apps using FPC since like 15 years ago. I've never need PHP or nodeJS or any non- Pascal language for that. Now it's even better, Free Pascal has a sister project named pas2js that convert pascal program into javascript. So, you can even write the front-end side using pascal as well. Isn't that awesome? 😊 First off, I am an old hand so I do not know much about "webapps", what are these? Do you mean one can use the Lazarus desktop programming and somehow show the programs through a web server? Regarding Javascript I am totally unfamiliar with Java so I'd rather stay away from that. Started using Pascal back in the 1980-es, though. ---------------------------------------------------------------------- MarkMLl · 2019-12-15 18:06 Bo, first step: find out what scripting etc. facilities are supported by your webserver. Second step: do not under any circumstances be tempted to write your own custom webserver. MarkMLl ---------------------------------------------------------------------- lainz · 2019-12-15 19:14 Quote from: BosseB on December 15, 2019, 07:03:49 pmRegarding Javascript I am totally unfamiliar with Java so I'd rather stay away from that. Started using Pascal back in the 1980-es, though. Javascript is not Java. ---------------------------------------------------------------------- BosseB · 2019-12-15 20:20 I now looked (briefly) at the links provided and it seems like a webapp is more or less an invisible processing system. The examples I looked at looked mostly like "Hello world" type programs operating in a console. But I need a GUI configuration form where I can enter or select values and then submit them to a web server handler. For me this has always been a php file receiving the data and then doing something with them. In the current case I want to check the validity of the data and store in the database or maybe trigger some server side action. Also on request from the user retrieve result data as files or display. This would be PHP code calling pascal programs for retrieval and then rendering the result into a GUI form sent back to the user. Rather than programming these database and file system operations in PHP my thought was to use PHP to extract the input data from the user (form submission) and then repackage these into a call to my pascal program that does the real work towards the database and hardware.... ---------------------------------------------------------------------- MarkMLl · 2019-12-15 20:31 As I said, you start off by finding out what facilities your web server (Apache?) is configured to provide. It might even have PHP. Any HTML page- without Javascript and potentially without CSS- has a basic set of interactive data entry components. Even without PHP etc., CSS allows you to make components visible/invisible depending on what else is going on. Javascript is vastly overused in my opinion, mainly by addons which benefit their writer more than the user (e.g. targetted advertising under the control of Google et al.). But detailed discussion is way off topic. MarkMLl ---------------------------------------------------------------------- lucamar · 2019-12-15 20:36 Quote from: BosseB on December 15, 2019, 09:20:20 pmRather than programming these database and file system operations in PHP my thought was to use PHP to extract the input data from the user (form submission) and then repackage these into a call to my pascal program that does the real work towards the database and hardware.... It depends heavily on what you're currently doing (if anything) and what your final goal is but if I were you I would forget about PHP and let my Pascal program manage everything. That way you can avoid most (if not all) problems of interoperabilrity between the two programs. Whether the backend is CGI/FastCGI, an Apache module or whatever I'm fairly sure it can be done in Pascal without more than the normal problems. ---------------------------------------------------------------------- BosseB · 2019-12-15 21:30 Quote from: MarkMLl on December 15, 2019, 09:31:09 pmAs I said, you start off by finding out what facilities your web server (Apache?) is configured to provide. It might even have PHP. As I think I have mentioned but possibly in another thread the target for my system is a Raspberry Pi running Raspbian Buster and with Apache2 + PHP etc installed. Not MySql though, I decided against its complications and am using an SQLite database instead. The web interface is not a user interface as such, it is just a front end to set up (configure) the system and define the automatic tasks to execute. In order to use it one has to be physically connected to the RPi network.QuoteAny HTML page- without Javascript and potentially without CSS- has a basic set of interactive data entry components. Even without PHP etc., CSS allows you to make components visible/invisible depending on what else is going on.Yes, I have designed a prototype webpage in php lingo that shows the boxes, lists and selectors I need. It works ok right now but it does not do anything of course. I think I will just use the PHP exec() function with my own pascal programs and get it done.... Of course I will have to create the php webpages also, but it should not be a big deal. And I already know the way to extract the posted data from the form so I can stuff it onto a call to my pascal program. Will do some experimentation shortly. ---------------------------------------------------------------------- bee · 2019-12-16 01:50 It seems Bosse doesn't have proper knowledge about web apps nor web pages. I wrote a tutorial about that, but unfortunately it's written in Bahasa Indonesia not English, because it's for my fellow Pascal programmers in Indonesia. The tutorial explains from the very basic of HTTP, HTML, CSS, JavaScript, scripting, web server features (based on Apache), communication between web server and our program (in Pascal), etc. PHP is just ONE of MANY ways to make a web apps. There are many other better ways than using PHP for web apps. Web apps are like desktop app but using web interface (instead of GUI or CLI). So, instead using OS GUI APIs to display things to user, it's using HTML and CSS and using Javascript to make to do the logic (on the browser side). Oh, Javascript is NOT Java. Basically, a web app starts from a browser request to your web server. Your web server should know the URL requested is handled by other application. Your web server forward the request to the application handler which is your pascal program. Your program then reads the input given by your web server (using either CGI, FastCGI, or Apache module). The easiest way is using CGI since all you need to do is read input from standard input and send output to standard output. Your program response to your web server request by sending output in HTML document along with the required CSS and Javascript code (either internal or external to the HTML document). Your web server reads your response and forward it to the browser that requested at the beginning. That's what happened in every single request and response of HTTP communication. Some people think that the application part (the program that you wrote to handle web server request) can only be written in PHP. It's very wrong. You can even write it in bash script or batch file. There's nothing that PHP can do and other language can't. My first web app was written in Turbo Pasal 5.5. ---------------------------------------------------------------------- bee · 2019-12-16 01:55 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. ---------------------------------------------------------------------- valdir.marcos · 2019-12-16 05:03 Quote from: bee on December 16, 2019, 02:55:00 amAlong 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.Impressive. Thanks for sharing. ---------------------------------------------------------------------- avra · 2019-12-16 07:59 You can simply add JsonRPC to your application and expose some of it's functionality to 3rd parties. If you browse fpc sources you will find some examples. There is also CeosMW JSONRPC which can be downloaded via OPM or through https://github.com/jbsolucoes/ceosmw. CeosMW components make this task very easy. ---------------------------------------------------------------------- PascalDragon · 2019-12-16 08:05 Quote from: BosseB on December 15, 2019, 09:20:20 pmBut I need a GUI configuration form where I can enter or select values and then submit them to a web server handler. For me this has always been a php file receiving the data and then doing something with them. PHP essentially boils down to reacting to a HTTP request (e.g. GET index.php) by sending a HTTP response to the client which (in most cases) will simply be HTML text. That is nothing special of PHP and can just as easily be down with FPC (or any other language in fact). ---------------------------------------------------------------------- MarkMLl · 2019-12-16 08:19 Quote from: bee on December 16, 2019, 02:50:28 amSome people think that the application part (the program that you wrote to handle web server request) can only be written in PHP. It's very wrong. You can even write it in bash script or batch file. There's nothing that PHP can do and other language can't. My first web app was written in Turbo Pasal 5.5. The crucially important thing is that the web server should have a mechanism to pass a request for certain content to custom code. That custom code could be PHP, SSI (in the original page), CGI (a script in a separate directory), or a server-specific mechanism to improve the efficiency of any of those (e.g. Apache's ModPerl, which eliminates the overhead of a load/compile every time a script is run). MarkMLl ---------------------------------------------------------------------- BosseB · 2019-12-16 10:36 Quote from: bee on December 16, 2019, 02:50:28 amIt seems Bosse doesn't have proper knowledge about web apps nor web pages. You are quite right about that concerning "Web apps"! It is the main reason I am asking here... Web pages in general I do know, I have written sites since the 1990:es using just a text editor...Quote I wrote a tutorial about that, but unfortunately it's written in Bahasa Indonesia not English, because it's for my fellow Pascal programmers in Indonesia. The tutorial explains from the very basic of HTTP, HTML, CSS, JavaScript, scripting, web server features (based on Apache), communication between web server and our program (in Pascal), etc. Maybe I can try sending it through Google Translate? QuotePHP is just ONE of MANY ways to make a web apps. There are many other better ways than using PHP for web apps. Web apps are like desktop app but using web interface (instead of GUI or CLI). So, instead using OS GUI APIs to display things to user, it's using HTML and CSS and using Javascript to make to do the logic (on the browser side). Oh, Javascript is NOT Java. I looked at your example and realized that: 1) One can use a straight pascal cli program to handle the POST or GET call. 2) One just has to place the program into the cgi-bin dir of the webserver and set its name as the form handler 3) One builds the response page as html in pascal code and put return variable values into that then writes it out as a writeln So I should be able to do this in my command line programs written with Lazarus! QuoteThe easiest way is using CGI since all you need to do is read input from standard input and send output to standard output. Your program response to your web server request by sending output in HTML document along with the required CSS and Javascript code (either internal or external to the HTML document). Your web server reads your response and forward it to the browser that requested at the beginning. That's what happened in every single request and response of HTTP communication. Some people think that the application part (the program that you wrote to handle web server request) can only be written in PHP. It's very wrong. You can even write it in bash script or batch file. There's nothing that PHP can do and other language can't. My first web app was written in Turbo Pasal 5.5. Very helpful response! Thank you very much! I have a few clarification questions: 1) Displaying the main form using a pascal program? Since I am using web forms for my configuration and status pages it would be better if I can synchronize the web form and processing inside the pascal program by having the actual page display also controlled by the pascal code. One less thing to get out of sync.... So can I display my config using an url such as this: Code: [Select]http://rpimonitor/cgi-bin/mainpageWhere mainpage is my pascal program for showing the main page... Or maybe cgi-bin is not directly available on the browser (I think it is typically located above the document root) so I may need an index.php file on the server that calls the cgi-bin script? If so how would that be done? 2) Form handler type GET and POST? The two types I know of are POST and GET where the submit button will in the GET case put the input data into the URL itself whereas in the POST case the data is sent in another way, which I don't know how... When dealing with form handlers there are different ways to deal with that in PHP, how does it work in cgi pascal programs? Say I use the POST method in the form, how do the data arrive into the pascal cmd line program? Grateful for your input!" ---------------------------------------------------------------------- MarkMLl · 2019-12-16 10:54 Quote from: BosseB on December 16, 2019, 11:36:03 amI looked at your example and realized that: 1) One can use a straight pascal cli program to handle the POST or GET call. 2) One just has to place the program into the cgi-bin dir of the webserver and set its name as the form handler 3) One builds the response page as html in pascal code and put return variable values into that then writes it out as a writeln So I should be able to do this in my command line programs written with Lazarus! That's right. But you'll need to look at the CGI environment variables that are made available to your script, and different servers make different one available. > Or maybe cgi-bin is not directly available on the browser (I think it is > typically located above the document root) so I may need an index.php > file on the server that calls the cgi-bin script? If so how would that be done? Note that cgi-bin is entirely a server-side thing. On the browser you've got Javascript (disabled to varying extents by security addons), and very rarely you might come across Java, ActiveX and so on. > 2) Form handler type GET and POST? One pipes the submission, the other encodes it in the URI. There have also been various server-specific methods in the past, e.g. PATCH, but those are rarely implemented these days. MarkMLl ---------------------------------------------------------------------- BosseB · 2019-12-16 11:32 I found this Apache tutorial concerning CGI and its various aspects... So I think I am on the right way now.  I believe that my compiled pascal program will work if it is placed in a dir, which has been marked in the apache conf file using the directive ScriptAlias. I will check that as soon as I have an example program to test with. Seems like the input data are available like this: POST: on STDIN as a string like name=value&name2=value&name3=value GET: in an environment variable QUERY_STRINGS in the same format as above ---------------------------------------------------------------------- MarkMLl · 2019-12-16 11:46 On a standard Debian system I found that Apache was configured to expect CGIs in /usr/lib/cgi-bin and that provided I set up a symlink it worked fine; I'd be surprised if Raspbian were different. I ended up with /var/www/html/cgi-bin which isn't quite the classic layout but if it works... BE TOLD: treat QUERY_STRINGS and the rest with caution. I think that one's fairly universally implemented, but there are others relating to referring document etc. which you'll need and are somewhat variable. MarkMLl ---------------------------------------------------------------------- bee · 2019-12-16 16:16 Quote from: BosseB on December 16, 2019, 11:36:03 amMaybe I can try sending it through Google Translate? If you're so interested, you may get my free mini ebook about web programming using (Free)Pascal here (it's a 2.2 MB PDF file). And the code of the examples of the book are here. Quote from: BosseB on December 16, 2019, 11:36:03 amI looked at your example and realized that: 1) One can use a straight pascal cli program to handle the POST or GET call. 2) One just has to place the program into the cgi-bin dir of the webserver and set its name as the form handler 3) One builds the response page as html in pascal code and put return variable values into that then writes it out as a writeln So I should be able to do this in my command line programs written with Lazarus! Indeed! Quote from: BosseB on December 16, 2019, 11:36:03 amVery helpful response! Thank you very much! You're welcome. I'm glad to be able to help you. Quote from: BosseB on December 16, 2019, 11:36:03 amI have a few clarification questions: 1) Displaying the main form using a pascal program? Since I am using web forms for my configuration and status pages it would be better if I can synchronize the web form and processing inside the pascal program by having the actual page display also controlled by the pascal code. One less thing to get out of sync.... So can I display my config using an url such as this: http://rpimonitor/cgi-bin/mainpage Where mainpage is my pascal program for showing the main page… The main page of your program is the output of the program itself. If your mainpage[.cgi] produce correct HTML document along with the required CSS and JS files, then the client browser will render it to the user like a normal web page. Quote from: BosseB on December 16, 2019, 11:36:03 amOr maybe cgi-bin is not directly available on the browser (I think it is typically located above the document root) so I may need an index.php file on the server that calls the cgi-bin script? If so how would that be done? The cgi-bin folder is configurable. It's the folder that specially set for F/CGI binary files, by default. Of course you may change it to any folder. Just study the Apache setting for executable binary files. Quote from: BosseB on December 16, 2019, 11:36:03 am2) Form handler type GET and POST? The two types I know of are POST and GET where the submit button will in the GET case put the input data into the URL itself whereas in the POST case the data is sent in another way, which I don't know how… When dealing with form handlers there are different ways to deal with that in PHP, how does it work in cgi pascal programs? Say I use the POST method in the form, how do the data arrive into the pascal cmd line program? Very simple actually. Every GET variables and its value will be stored in an environment variable named QUERY_STRING, you can use the GetEnvironmentVariable() function to read the content. And POST variables and its values streamed into standard input, you simply use the standard readln procedure to get the content. Or, you better study my tutorial and read the examples. Good luck! ---------------------------------------------------------------------- MarkMLl · 2019-12-16 17:30 Quote from: bee on December 16, 2019, 05:16:19 pmThe main page of your program is the output of the program itself. If your mainpage[.cgi] produce correct HTML document along with the required CSS and JS files, then the client browser will render it to the user like a normal web page. Or the main page can be a frameset, with at least some of the constituent frames being generated by CGI. That has the advantage that the scripts can look at the referrer and have some degree of confidence that they're not being fed malicious parameters, rather than letting a potentially-untrusted user drive the scripts directly. MarkMLl ---------------------------------------------------------------------- zamronypj · 2019-12-17 05:57 Quote from: BosseB on December 15, 2019, 06:12:16 pm Question: Is there some simple way to call processing functions written in Pascal from within PHP such that it can retrieve data to display to the user or execute commands and feed back results? All I really want is read and write to the SQLite database and also scan a few directories for certain files, list and down/upload these. Has someone here been there and done that and is willing to share the experience? mixing Pascal with PHP is actually hard. You need to export functionality written in Pascal as library (as shared object *.so).create PHP extension so that library function can be invoked from PHP code.Enable that extension Writing PHP extension requires you to use C language or Zephir language. I do not know if Free Pascal can be used to write PHP extension. (maybe Free Pascal can but, AFAIK, no PHP headers are converted yet) So the simplest way is just to use Pascal or PHP. PHP can access SQLite database as well as Free Pascal. ---------------------------------------------------------------------- egsuh · 2019-12-17 06:47 Once you have web-server program, then you may use Lazarus program (I mean Windows visual program if the terminals are running on Windows --- I do not know about Linux) using Synapse... You don't have to bother write HTML (and CSS, JS, etc.). ---------------------------------------------------------------------- avra · 2019-12-17 07:31 Quote from: zamronypj on December 17, 2019, 06:57:32 ammixing Pascal with PHP is actually hard. Not if you use JsonRPC on Pascal side (like CeosMW), and JsonRPC on PHP side (like https://github.com/datto/php- json-rpc). ---------------------------------------------------------------------- PascalDragon · 2019-12-17 08:23 Quote from: BosseB on December 16, 2019, 11:36:03 amI looked at your example and realized that: 1) One can use a straight pascal cli program to handle the POST or GET call. 2) One just has to place the program into the cgi-bin dir of the webserver and set its name as the form handler 3) One builds the response page as html in pascal code and put return variable values into that then writes it out as a writeln So I should be able to do this in my command line programs written with Lazarus! You can also take a look at fpWeb which is part of FPC and Lazarus and would deal with all the background stuff of request variables, headers, etc. You can find an example for CGI here. Please note that you do not need to use Lazarus for this. You can just as well use fpWeb inside a "command line" application. ---------------------------------------------------------------------- BosseB · 2019-12-17 10:34 Quote from: PascalDragon on December 17, 2019, 09:23:18 amQuote from: BosseB on December 16, 2019, 11:36:03 amI looked at your example and realized that: 1) One can use a straight pascal cli program to handle the POST or GET call. 2) One just has to place the program into the cgi-bin dir of the webserver and set its name as the form handler 3) One builds the response page as html in pascal code and put return variable values into that then writes it out as a writeln So I should be able to do this in my command line programs written with Lazarus! You can also take a look at fpWeb which is part of FPC and Lazarus and would deal with all the background stuff of request variables, headers, etc. You can find an example for CGI here. Please note that you do not need to use Lazarus for this. You can just as well use fpWeb inside a "command line" application. When I am referring to Lazarus I am just indicating my use of it as a development IDE. The actual programs are just non-visual command line programs communicationg via start arguments, environment variables and STDIO interchanges. Meant to run on an RPi without anyone logged on and no display or keyboard attached. Also the RPi will be interfaced this way only for configuration by a single user and once that is done it will be on its own... And I have scrapped the idea of using PHP in favour of directly calling the pascal programs from Apache. So the subject of this therad is now a bit misleading.... ---------------------------------------------------------------------- mr-highball · 2019-12-17 14:59 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 ---------------------------------------------------------------------- BosseB · 2019-12-17 18:55 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: The data sent out from the program looks like this: Code: [Select]Content-type: text/html Monitoring Config

Got Data:
namn=Olle
trafik=hemsk
Test=7

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 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? ---------------------------------------------------------------------- lucamar · 2019-12-17 19:20 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. ---------------------------------------------------------------------- MarkMLl · 2019-12-17 19:21 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]         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 ---------------------------------------------------------------------- MarkMLl · 2019-12-17 19:29 Quote from: lucamar on December 17, 2019, 08:20:02 pmYou 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. ---------------------------------------------------------------------- dsiders · 2019-12-17 19:55 Quote from: BosseB on December 17, 2019, 07:55:05 pmBut 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: The data sent out from the program looks like this: Code: [Select]Content-type: text/html Monitoring Config

Got Data:
namn=Olle
trafik=hemsk
Test=7

Using a valid DOCTYPE would probably help: HTML 5 Code: XML  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i HTML 4.01 Strict Code: XML  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i HTML 4.01 Transitional Code: XML  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i HTML 4.01 Frameset Code: XML  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i XHTML 1.0 Strict Code: XML  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i XHTML 1.0 Transitional Code: XML  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   XHTML 1.0 Frameset Code: XML  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i XHTML 1.1 Code: XML  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i AddDefaultCharset utf-8or Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i AddCharset utf-8 .htm .html .js .css Hope that helps... ---------------------------------------------------------------------- bee · 2019-12-18 02:26 The most minimalist code for CGI is as simple as this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program test; begin  writeln('content-type: text/html;');  writeln;  writeln('Hello world!');end. Compiled with FPC 3.0.4. It works perfectly fine on my Ubuntu + Apache server here: https://pak.lebah.web.id/test.cgi ---------------------------------------------------------------------- BosseB · 2019-12-18 07:27 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 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i make cleanmake bigide OPT=-dFPC_ARMHF FPC=/home/pi/bin/ppcarmThen 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..... ---------------------------------------------------------------------- MarkMLl · 2019-12-18 08:12 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 ---------------------------------------------------------------------- bee · 2019-12-18 13:06 Quote from: BosseB on December 18, 2019, 08:27:39 amI 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. Quote from: BosseB on December 18, 2019, 08:27:39 amThen 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. ---------------------------------------------------------------------- BosseB · 2019-12-18 16:05 Quote from: bee on December 18, 2019, 02:06:01 pmThis 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...  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... QuoteUnless 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... QuoteWell, 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. ---------------------------------------------------------------------- bee · 2019-12-19 01:11 Quote from: BosseB on December 18, 2019, 05:05:47 pmMy current usage is for exactly 1 user...  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. Quote from: BosseB on December 18, 2019, 05:05:47 pmNow 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. Quote from: BosseB on December 18, 2019, 05:05:47 pmFrom 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. Quote from: BosseB on December 18, 2019, 05:05:47 pmI 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. ---------------------------------------------------------------------- PaulRowntree · 2019-12-19 03:19 Quote from: bee on December 16, 2019, 02:55:00 amAlong 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! ---------------------------------------------------------------------- ThomasK · 2021-09-17 15:51 Quote from: BosseB on December 18, 2019, 05:05:47 pmQuote from: bee on December 18, 2019, 02:06:01 pmThis 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...  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... QuoteUnless 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... QuoteWell, 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. Hi Bo, can you provide me with the translations? - I want to do more or less the same thing, a single page with data from my solar system. Thanks and Best Regards, Thomas ---------------------------------------------------------------------- ojz0r · 2021-09-17 19:04 ThomasK: I've got something similar running right now but presenting temperature sensor data on a webserver and also controlling my house district heating system setpoint. Everything running with only pascal on my raspberry pi zero w. The page itself is html with values in html id's changed with an exported javascript file which is exported on http request and served. ---------------------------------------------------------------------- devEric69 · 2021-09-18 08:28 AMHO, use this https://wiki.freepascal.org/fpWeb_Tutorial or use this https://www.php.net/manual/fr/index.php. Mixing the two isn't recommended, if its possible: be aware that mixing the 2 languages means that the CGI-Pascal-fpWeb named session - said PASCAL_SESSID, or whatever you want - knows how to retrieve the Php named session - said PHP_SESSID, or whatever you want - in order to be assigned with its\some web session variables (using embedded COOKIE or URL's GET or URL's POST shuttle), or vice versa. Absolutely possible. But this complicates things. nb1: Php was created as a *.htm template handling laguage. For information, fpWeb has a *.htm template engine named fpTemplates. nb2: CGI-Pascal-fpWeb is just a console program.