Bitcoin Forum
October 01, 2016, 04:56:04 AM *
News: Due to DDoS attacks, there may be periodic downtime.
 
   Home   Help Search Donate Login Register  
Pages: [1]
  Print  
Author Topic: Simple CGI Question?  (Read 1422 times)
semyazza
Sr. Member
****
Offline Offline

Activity: 339


View Profile
August 18, 2010, 07:25:26 PM
 #1

Hello,
I'm having a little trouble parsing and displaying the output from the bitcoind client in the browser from a CGI program written in C.
Code:

#include <stdio.h>
main()
{
/* Always print a content type and a blank line. */
printf("Content-Type: text/html\n\n");

/* HTML Page Start */
puts("<html>");
puts("<head>");
puts("<title>");
puts("</title>");
puts("<body>");


int entry = 1;
char line[200];
FILE* output = popen("/var/www/bitcoind.cgi getblockcount", "r");
while ( fgets(line, 199, output) )
{
printf("%5d: %s", entry++, line);
}

puts("</body>");
puts("</html>");

return 0;
}


Is producing only the following HTML code through the cgi interface:
Code:

<html>
<head>
<title>
</title>
<body>
</body>
</html>

The command line output when run in a terminal is(which is correct):
Code:
<html>
<head>
<title>
</title>
<body>
1: 75029
</body>
</html>


Any ideas? I know I'm missing something simple.
1475297764
Hero Member
*
Offline Offline

Posts: 1475297764

View Profile Personal Message (Offline)

Ignore
1475297764
Reply with quote  #2

1475297764
Report to moderator
1475297764
Hero Member
*
Offline Offline

Posts: 1475297764

View Profile Personal Message (Offline)

Ignore
1475297764
Reply with quote  #2

1475297764
Report to moderator
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction. Advertise here.
1475297764
Hero Member
*
Offline Offline

Posts: 1475297764

View Profile Personal Message (Offline)

Ignore
1475297764
Reply with quote  #2

1475297764
Report to moderator
1475297764
Hero Member
*
Offline Offline

Posts: 1475297764

View Profile Personal Message (Offline)

Ignore
1475297764
Reply with quote  #2

1475297764
Report to moderator
jgarzik
Legendary
*
qt
Offline Offline

Activity: 1470


View Profile
August 18, 2010, 07:35:50 PM
 #2

   FILE* output = popen("/var/www/bitcoind.cgi getblockcount", "r");

Seems like an incorrect execution string.


Jeff Garzik, bitcoin core dev team and BitPay engineer; opinions are my own, not my employer.
Donations / tip jar: 1BrufViLKnSWtuWGkryPsKsxonV2NQ7Tcj
BioMike
Legendary
*
Offline Offline

Activity: 1218


View Profile
August 18, 2010, 09:04:36 PM
 #3

right permissions?
semyazza
Sr. Member
****
Offline Offline

Activity: 339


View Profile
August 19, 2010, 02:21:08 PM
 #4

   FILE* output = popen("/var/www/bitcoind.cgi getblockcount", "r");

Seems like an incorrect execution string.


Execution string seems to be right as it is runs in the terminal fine.
semyazza
Sr. Member
****
Offline Offline

Activity: 339


View Profile
August 19, 2010, 02:22:18 PM
 #5

right permissions?
Permissions are correct.  Owner and group are the webserver and for testing purposes it is 777.
silverman
Jr. Member
*
Offline Offline

Activity: 59


View Profile
August 19, 2010, 03:56:59 PM
 #6

Content-type: text/html\r\n ?

(or maybe it's Content-type: text/html\r\n\r\n, it's really fussy, and I forget.)

semyazza
Sr. Member
****
Offline Offline

Activity: 339


View Profile
August 19, 2010, 09:49:19 PM
 #7

Content-type: text/html\r\n ?

(or maybe it's Content-type: text/html\r\n\r\n, it's really fussy, and I forget.)


The recommendation is a double line break of \n\n.
http://www.w3.org/International/O-HTTP-charset

I still can't figure this out :-(... anyone?  It should be really simple. I even tried piping the output to a text file and it will not output correctly when run through CGI. I'm using Apache 2.2 if that helps anyone.
BioMike
Legendary
*
Offline Offline

Activity: 1218


View Profile
August 20, 2010, 05:12:26 AM
 #8

Do the server logs say anything useful?
lfm
Full Member
***
Offline Offline

Activity: 196



View Profile
August 20, 2010, 08:44:44 AM
 #9

right permissions?
Permissions are correct.  Owner and group are the webserver and for testing purposes it is 777.

check the return values from the open. is it NULL?
semyazza
Sr. Member
****
Offline Offline

Activity: 339


View Profile
August 20, 2010, 02:04:58 PM
 #10

Do the server logs say anything useful?
I failed debugging 101 somewhere. The error log did indeed give me useful information.

What happened:
The bitcoind client when run acts as an RPC client and connects to the already running instance of bitcoind to get its data(I did not know that). Also, the default apache configuration uses / as its home directory not the same directory the bitcoind client uses to store the bitcoin.conf file. When bitcoind was run with a command switch from the CGI it was looking in the home directory of the user it was run from(the webserver) for bitcoin.conf and not from the home directory of the user bitcoind was running as.

Fixes:
1) Have bitcoind and the webserver run as the same user so they share the same configuration directory
2) Create a link between the bitcoin.conf from the home directories of the users(webserver and bitcoind user)
3) Create a duplicate configuration file for both users.

Thanks for the help everyone!
xcikni
Newbie
*
Offline Offline

Activity: 3


View Profile
August 23, 2010, 03:07:05 AM
 #11

Have you tried closing the <head> tag?
BioMike
Legendary
*
Offline Offline

Activity: 1218


View Profile
August 23, 2010, 05:08:43 AM
 #12

Have you tried closing the <head> tag?

FYI. The problem was already solved.
Pages: [1]
  Print  
 
Jump to:  

Sponsored by , a Bitcoin-accepting VPN.
Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!