CGI issue (SOLVED)

Just starting out? Need help? Post your questions and find answers here.
thanos
Enthusiast
Enthusiast
Posts: 390
Joined: Sat Jan 12, 2008 3:25 pm
Location: Greece
Contact:

CGI issue (SOLVED)

Post by thanos »

Hello.
I tried to write a small cgi program in PB 5.71 x86 for Windows and 5.71 x64 for Linux (Mint).
I tested it under XAMPP in Windows and at my website in Linux environments.
Both servers return the result:
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator at postmaster@localhost to inform them of the time this error occurred, and the actions you performed just before this error.

More information about this error may be available in the server error log.

Apache/2.4.46 (Win64) OpenSSL/1.1.1g PHP/7.2.34 Server at localhost Port 80.

The permissions for Linux was set at 755.
Then I just tried to compile the CGI example from the manual.
Same error for Windows (I did not tried at Linux yet).
I also tried a small ccgi code in C in the same Windows environment.
It worked smoothly.
Any idea?
Last edited by thanos on Fri Jan 01, 2021 10:12 pm, edited 1 time in total.
» myPersonal Banker :: Because you do not need to have a master degree in economics in order to organize your finances!
DarkDragon
Addict
Addict
Posts: 2124
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Re: CGI issue

Post by DarkDragon »

Is it marked executable for the user under which the server software is running? Some servers only execute executables inside specific directories such as cgi-bin. Can you show us some code?
bye,
Daniel
thanos
Enthusiast
Enthusiast
Posts: 390
Joined: Sat Jan 12, 2008 3:25 pm
Location: Greece
Contact:

Re: CGI issue

Post by thanos »

DarkDragon wrote:Is it marked executable for the user under which the server software is running? Some servers only execute executables inside specific directories such as cgi-bin. Can you show us some code?
The file is in cgi-bin directory in both cases.
In windows its name is cgi_test.exe (i tried with cgi_test.cgi also) and in linux is cgi_test.cgi
The file is compiled as a console executable.
As i already said the code is just the example from the manual:

Code: Select all

If Not InitCGI() Or Not ReadCGI()
  End
EndIf

WriteCGIHeader(#PB_CGI_HeaderContentType, "text/html", #PB_CGI_LastHeader) ; Write the headers to inform the browser of the content format

WriteCGIString("<html><title>PureBasic CGI</title><body>")  

Procedure WriteCGIConstant(Constant$)
  WriteCGIString(Constant$ + ": " + CGIVariable(Constant$)+"<br>")
EndProcedure

WriteCGIConstant(#PB_CGI_AuthType)
WriteCGIConstant(#PB_CGI_ContentLength)
WriteCGIConstant(#PB_CGI_HeaderContentType)
WriteCGIConstant(#PB_CGI_DocumentRoot)
WriteCGIConstant(#PB_CGI_GatewayInterface)
WriteCGIConstant(#PB_CGI_PathInfo)
WriteCGIConstant(#PB_CGI_PathTranslated)
WriteCGIConstant(#PB_CGI_QueryString)
WriteCGIConstant(#PB_CGI_RemoteAddr)
WriteCGIConstant(#PB_CGI_RemoteHost)
WriteCGIConstant(#PB_CGI_RemoteIdent)
WriteCGIConstant(#PB_CGI_RemotePort)
WriteCGIConstant(#PB_CGI_RemoteUser)
WriteCGIConstant(#PB_CGI_RequestURI)
WriteCGIConstant(#PB_CGI_RequestMethod)
WriteCGIConstant(#PB_CGI_ScriptName)
WriteCGIConstant(#PB_CGI_ScriptFilename)
WriteCGIConstant(#PB_CGI_ServerAdmin)
WriteCGIConstant(#PB_CGI_ServerName)
WriteCGIConstant(#PB_CGI_ServerPort)
WriteCGIConstant(#PB_CGI_ServerProtocol)
WriteCGIConstant(#PB_CGI_ServerSignature)
WriteCGIConstant(#PB_CGI_ServerSoftware)
WriteCGIConstant(#PB_CGI_HttpAccept)
WriteCGIConstant(#PB_CGI_HttpAcceptEncoding)
WriteCGIConstant(#PB_CGI_HttpAcceptLanguage)
WriteCGIConstant(#PB_CGI_HttpCookie)
WriteCGIConstant(#PB_CGI_HttpForwarded)
WriteCGIConstant(#PB_CGI_HttpHost)
WriteCGIConstant(#PB_CGI_HttpPragma)
WriteCGIConstant(#PB_CGI_HttpReferer)
WriteCGIConstant(#PB_CGI_HttpUserAgent)

WriteCGIString("</body></html>")
Just copy and paste.
» myPersonal Banker :: Because you do not need to have a master degree in economics in order to organize your finances!
dige
Addict
Addict
Posts: 1102
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

Re: CGI issue

Post by dige »

Internal Server Error sounds like a crash of the cgi executable. Have you ever run the exe directly?
Otherwise you could test if 32bit or 64bit compiled helps..
"Daddy, I'll run faster, then it is not so far..."
DarkDragon
Addict
Addict
Posts: 2124
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Re: CGI issue

Post by DarkDragon »

Did you check the permissions and ownership/group of the file? What do the server logs say? (Access log & error log)
bye,
Daniel
thanos
Enthusiast
Enthusiast
Posts: 390
Joined: Sat Jan 12, 2008 3:25 pm
Location: Greece
Contact:

Re: CGI issue

Post by thanos »

dige wrote:Internal Server Error sounds like a crash of the cgi executable. Have you ever run the exe directly?
Otherwise you could test if 32bit or 64bit compiled helps..
Both exe (x86 and x64) files run directly without problem. But , as CGI executables both returns "Internal Server Error"
» myPersonal Banker :: Because you do not need to have a master degree in economics in order to organize your finances!
DarkDragon
Addict
Addict
Posts: 2124
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Re: CGI issue

Post by DarkDragon »

thanos wrote:
dige wrote:Internal Server Error sounds like a crash of the cgi executable. Have you ever run the exe directly?
Otherwise you could test if 32bit or 64bit compiled helps..
Both exe (x86 and x64) files run directly without problem. But , as CGI executables both returns "Internal Server Error"
Error and access logs of the server software typically show more about the error.
bye,
Daniel
thanos
Enthusiast
Enthusiast
Posts: 390
Joined: Sat Jan 12, 2008 3:25 pm
Location: Greece
Contact:

Re: CGI issue

Post by thanos »

DarkDragon wrote:
thanos wrote:
dige wrote:Internal Server Error sounds like a crash of the cgi executable. Have you ever run the exe directly?
Otherwise you could test if 32bit or 64bit compiled helps..
Both exe (x86 and x64) files run directly without problem. But , as CGI executables both returns "Internal Server Error"
Error and access logs of the server software typically show more about the error.
I tried to call both x86 and x64 versions.
Same error:
[Tue Dec 29 22:50:03.154918 2020] [cgi:error] [pid 7600:tid 1736] [client ::1:49236] End of script output before headers: a_test.exe
[Tue Dec 29 22:50:27.286824 2020] [cgi:error] [pid 12492:tid 1700] [client ::1:49264] End of script output before headers: a_test64.exe
» myPersonal Banker :: Because you do not need to have a master degree in economics in order to organize your finances!
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1108
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Re: CGI issue

Post by Paul »

Certainly a problem with your Server setup.

I compiled the above example code as both 32bit and 64bit and ran on my Windows Server and Linux Server without issues.
Used the latest version PB 5.73 as well as PB 5.71 which is what you say you are using.

Are you trying to access your cgi-bin from the outside or locally?
Image Image
DarkDragon
Addict
Addict
Posts: 2124
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Re: CGI issue

Post by DarkDragon »

thanos wrote:
DarkDragon wrote:
thanos wrote:
dige wrote:Internal Server Error sounds like a crash of the cgi executable. Have you ever run the exe directly?
Otherwise you could test if 32bit or 64bit compiled helps..
Both exe (x86 and x64) files run directly without problem. But , as CGI executables both returns "Internal Server Error"
Error and access logs of the server software typically show more about the error.
I tried to call both x86 and x64 versions.
Same error:
[Tue Dec 29 22:50:03.154918 2020] [cgi:error] [pid 7600:tid 1736] [client ::1:49236] End of script output before headers: a_test.exe
[Tue Dec 29 22:50:27.286824 2020] [cgi:error] [pid 12492:tid 1700] [client ::1:49264] End of script output before headers: a_test64.exe
Try executing the compiled executables yourself in a command line and check the output with a text and a hex editor. The newlines should be carriage return and line feeds. It seems the output either is not correct or the server is not expecting that output. Do you also have the C code or a link to the other working example?
bye,
Daniel
Marc56us
Addict
Addict
Posts: 1108
Joined: Sat Feb 08, 2014 3:26 pm
Location: France

Re: CGI issue

Post by Marc56us »

thanos wrote: [Tue Dec 29 22:50:03.154918 2020] [cgi:error] [pid 7600:tid 1736] [client ::1:49236] End of script output before headers: a_test.exe
[Tue Dec 29 22:50:27.286824 2020] [cgi:error] [pid 12492:tid 1700] [client ::1:49264] End of script output before headers: a_test64.exe
When a program displays an error message, use it as it is.
Have you tried all the solutions proposed in these answers?
https://www.google.com/search?q=End+of+ ... re+headers
:wink:
(English is not my native language, I use an online translator.)
thanos
Enthusiast
Enthusiast
Posts: 390
Joined: Sat Jan 12, 2008 3:25 pm
Location: Greece
Contact:

Re: CGI issue

Post by thanos »

DarkDragon wrote:
thanos wrote:
DarkDragon wrote:
thanos wrote:
dige wrote:Internal Server Error sounds like a crash of the cgi executable. Have you ever run the exe directly?
Otherwise you could test if 32bit or 64bit compiled helps..
Both exe (x86 and x64) files run directly without problem. But , as CGI executables both returns "Internal Server Error"
Error and access logs of the server software typically show more about the error.
I tried to call both x86 and x64 versions.
Same error:
[Tue Dec 29 22:50:03.154918 2020] [cgi:error] [pid 7600:tid 1736] [client ::1:49236] End of script output before headers: a_test.exe
[Tue Dec 29 22:50:27.286824 2020] [cgi:error] [pid 12492:tid 1700] [client ::1:49264] End of script output before headers: a_test64.exe
Try executing the compiled executables yourself in a command line and check the output with a text and a hex editor. The newlines should be carriage return and line feeds. It seems the output either is not correct or the server is not expecting that output. Do you also have the C code or a link to the other working example?
The C code:

Code: Select all

#include <stdio.h>
#include <stdlib.h>

void send_CGI_header()
{
	printf("Content-type: text/html\n");
	printf("\n");
}


void send_CGI_start()
{
	printf("<!doctype html>\n");
	printf("<html>\n");
	printf("<body>\n");
	printf("<title>Trivial Example</title>\n");
	printf("<p>Trivial CGI example!\n");
  	printf("<p><a href=\"https://www.google.com\">Simple Hyperlink</a>\n");    
}

void send_CGI_end()
{
	printf("</body>\n");
	printf("</html>\n");
	printf("\n");
}


int main(int argc, char** argv)
{
	send_CGI_header();
	send_CGI_start();

    /* Query string */
    char *qs = getenv("QUERY_STRING" );
    if (qs != NULL)
    {
        printf( "<p>QUERY_STRING=> %s\n ", qs ? qs : "" );
        printf("\n");
    }

    send_CGI_end;
    
	return 0;
}
Which displays the following output:
Trivial CGI example!

Simple Hyperlink

QUERY_STRING=> mydata=42211-000027

The results are the same even I called direct (e.g.

Code: Select all

http://localhost/cgi-bin/cgi_trivial_test.exe?mydata=42211-000027
or indirect.
» myPersonal Banker :: Because you do not need to have a master degree in economics in order to organize your finances!
thanos
Enthusiast
Enthusiast
Posts: 390
Joined: Sat Jan 12, 2008 3:25 pm
Location: Greece
Contact:

Re: CGI issue

Post by thanos »

The same executable, compiled by tcc compiler for linux also worked in my site
» myPersonal Banker :: Because you do not need to have a master degree in economics in order to organize your finances!
thanos
Enthusiast
Enthusiast
Posts: 390
Joined: Sat Jan 12, 2008 3:25 pm
Location: Greece
Contact:

Re: CGI issue

Post by thanos »

Paul wrote:Certainly a problem with your Server setup.

I compiled the above example code as both 32bit and 64bit and ran on my Windows Server and Linux Server without issues.
Used the latest version PB 5.73 as well as PB 5.71 which is what you say you are using.

Are you trying to access your cgi-bin from the outside or locally?
Both ways.
The simple c code which i posted, worked in both environments (xampp for windows locally and linux hosted).
Direct and indirect.
I also tried to compile with PB 5.73 x64 for Linux. Same bad results.
Do you have any idea what I have to ask from hosting company?
I email them and they replied that is my executable's problem
» myPersonal Banker :: Because you do not need to have a master degree in economics in order to organize your finances!
DarkDragon
Addict
Addict
Posts: 2124
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Re: CGI issue

Post by DarkDragon »

I've compared the outputs now:

Code: Select all

00000000: 436f 6e74 656e 742d 7479 7065 3a20 7465  Content-type: te
00000010: 7874 2f68 746d 6c0a 0a3c 2164 6f63 7479  xt/html..<!docty
00000020: 7065 2068 746d 6c3e 0a3c 6874 6d6c 3e0a  pe html>.<html>.
00000030: 3c62 6f64 793e 0a3c 7469 746c 653e 5472  <body>.<title>Tr
00000040: 6976 6961 6c20 4578 616d 706c 653c 2f74  ivial Example</t
00000050: 6974 6c65 3e0a 3c70 3e54 7269 7669 616c  itle>.<p>Trivial
00000060: 2043 4749 2065 7861 6d70 6c65 210a 3c70   CGI example!.<p
00000070: 3e3c 6120 6872 6566 3d22 6874 7470 733a  ><a href="https:
00000080: 2f2f 7777 772e 676f 6f67 6c65 2e63 6f6d  //www.google.com
00000090: 223e 5369 6d70 6c65 2048 7970 6572 6c69  ">Simple Hyperli
000000a0: 6e6b 3c2f 613e 0a                        nk</a>.
vs

Code: Select all

00000000: 436f 6e74 656e 742d 7479 7065 3a20 7465  Content-type: te
00000010: 7874 2f68 746d 6c0a 0a3c 6874 6d6c 3e3c  xt/html..<html><
00000020: 7469 746c 653e 5075 7265 4261 7369 6320  title>PureBasic 
00000030: 4347 493c 2f74 6974 6c65 3e3c 626f 6479  CGI</title><body
00000040: 3e41 5554 485f 5459 5045 3a20 3c62 723e  >AUTH_TYPE: <br>
00000050: 434f 4e54 454e 545f 4c45 4e47 5448 3a20  CONTENT_LENGTH: 
00000060: 3c62 723e 436f 6e74 656e 742d 7479 7065  <br>Content-type
00000070: 3a20 3c62 723e 444f 4355 4d45 4e54 5f52  : <br>DOCUMENT_R
00000080: 4f4f 543a 203c 6272 3e47 4154 4557 4159  OOT: <br>GATEWAY
00000090: 5f49 4e54 4552 4641 4345 3a20 3c62 723e  _INTERFACE: <br>
000000a0: 5041 5448 5f49 4e46 4f3a 203c 6272 3e50  PATH_INFO: <br>P
000000b0: 4154 485f 5452 414e 534c 4154 4544 3a20  ATH_TRANSLATED: 
000000c0: 3c62 723e 5155 4552 595f 5354 5249 4e47  <br>QUERY_STRING
000000d0: 3a20 3c62 723e 5245 4d4f 5445 5f41 4444  : <br>REMOTE_ADD
000000e0: 523a 203c 6272 3e52 454d 4f54 455f 484f  R: <br>REMOTE_HO
000000f0: 5354 3a20 3c62 723e 5245 4d4f 5445 5f49  ST: <br>REMOTE_I
00000100: 4445 4e54 3a20 3c62 723e 5245 4d4f 5445  DENT: <br>REMOTE
00000110: 5f50 4f52 543a 203c 6272 3e52 454d 4f54  _PORT: <br>REMOT
00000120: 455f 5553 4552 3a20 3c62 723e 5245 5155  E_USER: <br>REQU
00000130: 4553 545f 5552 493a 203c 6272 3e52 4551  EST_URI: <br>REQ
00000140: 5545 5354 5f4d 4554 484f 443a 203c 6272  UEST_METHOD: <br
00000150: 3e53 4352 4950 545f 4e41 4d45 3a20 3c62  >SCRIPT_NAME: <b
00000160: 723e 5343 5249 5054 5f46 494c 454e 414d  r>SCRIPT_FILENAM
00000170: 453a 203c 6272 3e53 4552 5645 525f 4144  E: <br>SERVER_AD
00000180: 4d49 4e3a 203c 6272 3e53 4552 5645 525f  MIN: <br>SERVER_
00000190: 4e41 4d45 3a20 3c62 723e 5345 5256 4552  NAME: <br>SERVER
000001a0: 5f50 4f52 543a 203c 6272 3e53 4552 5645  _PORT: <br>SERVE
000001b0: 525f 5052 4f54 4f43 4f4c 3a20 3c62 723e  R_PROTOCOL: <br>
000001c0: 5345 5256 4552 5f53 4947 4e41 5455 5245  SERVER_SIGNATURE
000001d0: 3a20 3c62 723e 5345 5256 4552 5f53 4f46  : <br>SERVER_SOF
000001e0: 5457 4152 453a 203c 6272 3e48 5454 505f  TWARE: <br>HTTP_
000001f0: 4143 4345 5054 3a20 3c62 723e 4854 5450  ACCEPT: <br>HTTP
00000200: 5f41 4343 4550 545f 454e 434f 4449 4e47  _ACCEPT_ENCODING
00000210: 3a20 3c62 723e 4854 5450 5f41 4343 4550  : <br>HTTP_ACCEP
00000220: 545f 4c41 4e47 5541 4745 3a20 3c62 723e  T_LANGUAGE: <br>
00000230: 4854 5450 5f43 4f4f 4b49 453a 203c 6272  HTTP_COOKIE: <br
00000240: 3e48 5454 505f 464f 5257 4152 4445 443a  >HTTP_FORWARDED:
00000250: 203c 6272 3e48 5454 505f 484f 5354 3a20   <br>HTTP_HOST: 
00000260: 3c62 723e 4854 5450 5f50 5241 474d 413a  <br>HTTP_PRAGMA:
00000270: 203c 6272 3e48 5454 505f 5245 4645 5245   <br>HTTP_REFERE
00000280: 523a 203c 6272 3e48 5454 505f 5553 4552  R: <br>HTTP_USER
00000290: 5f41 4745 4e54 3a20 3c62 723e 3c2f 626f  _AGENT: <br></bo
000002a0: 6479 3e3c 2f68 746d 6c3e                 dy></html>
No difference in the header, but at the end it misses a newline. This might be essential if you do not send the content length. So check whether this helps:
Change

Code: Select all

WriteCGIString("</body></html>")
To

Code: Select all

WriteCGIStringN("</body></html>")
Simply pipe the output to xxd, this gives you the hex output like above if you want to compare the outputs. Another thing that might differ on your server is whether a library is installed or not, however I doubt that either libpthread or libdl is missing:

Code: Select all

ldd cgic.elf
	linux-vdso.so.1 (0x00007ffffe3ee000)
	libc.so.6 => /usr/lib/libc.so.6 (0x00007fa4a7996000)
	/lib64/ld-linux-x86-64.so.2 => /usr/lib64/ld-linux-x86-64.so.2 (0x00007fa4a7bb7000)
ldd cgipb.elf
	linux-vdso.so.1 (0x00007ffdae5fa000)
	libpthread.so.0 => /usr/lib/libpthread.so.0 (0x00007f82c5ee0000)
	libc.so.6 => /usr/lib/libc.so.6 (0x00007f82c5d17000)
	libdl.so.2 => /usr/lib/libdl.so.2 (0x00007f82c5d11000)
	/lib64/ld-linux-x86-64.so.2 => /usr/lib64/ld-linux-x86-64.so.2 (0x00007f82c5f55000)
On Windows you can use dependency walker instead of ldd.
bye,
Daniel
Post Reply