Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all

I have been working with an existing CGI application under linux with C++. The problem that on some pages it gives me the error

502 Bad Gateway
The CGI was not CGI 1.1 compliant

Before that it wrote on the same page "cgi_header: unable to find LFLF"!

What's more weird is the program starts correctly but when I go to specific pages I get this error. I tried to use an fstream object to log where the exact point of the crash but I found out that if I only write this

C++
ofstream ff("test.txt", ofstream::app);

// The Code
// That displays
// The page

// Last line
ff.close();


and even not writing anything to the stream just declare and close it will make the page run correctly!

The system uses the same code but with different parameters on some pages, the page's contents come form sqlite 3 database and the database data is correct and can be viewed , there are other pages that works perfectly on the same code but there are specific pages that stuck on this error :\ (in other words, same code on other pages work!)

I've checked everything regarding the code, database or HTML. I also searched online and all results tells the same thing, I should make sure to print an empty line after "Content-type:text/HTML" (cout << "Content-type:text/HTML\n\n";) and even this is correct in my code.

PS: This situation happens when I run the application via an embedded linux device I'm using, If I run it through the localhost on my PC everything works.

Any help is appreciated, Thanks!
Posted
Updated 6-Aug-14 4:21am
v3
Comments
KarstenK 6-Aug-14 12:13pm    
View at the network traffic with a tool like wireshark. There you will see what is really running over the network.
Mohibur Rashid 24-Sep-14 23:49pm    
did you start with printing content type? if you do not post content type, browser will fail.

Basic of HTTP is: Two types of data is send to client with every request, except HEADER Request.
1. Header
2. Data body
Every header must have to end with a new line.
After sending all header you will have to send a single new line. That will inform the client that header send finished.
The sequence of data:
HTTP server will have to send status code first.
example
HTTP/1.1 200 OK\n
This is done by server.
Next it sends other header information. The must send information is Content-Type.
Example: Content-Type: text/html; charset=UTF-8\n
after sending all header just send \n

Reading your problem my thought is, you did not add both content type and new line

1 solution

Refer to this
 
Share this answer
 
Comments
Mohibur Rashid 24-Sep-14 23:51pm    
this is an old post and answer is not related.
Abdallah Al-Dalleh 25-Sep-14 9:28am    
Anything needed to prepare the CGI header is correct in the code I have. Somehow the header gets not completed since in the middle of execution the program crashes due to an index out of range exception. I solved the issue and the CGI works fine, so I think anything related to CGI is configure correctly.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900