3 Replies - 3748 Views - Last Post: 12 February 2014 - 09:52 AM

#1 PinkJazzX  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 12-October 11

CGI - page not displaying, no output from script

Posted 11 February 2014 - 02:29 PM

In Java, I am trying to capture the output of a CGI script (written in C++) I try to open in my browser. Unfortunately, my browser doesn't display anything and I don't get any output in Java.

I have the following Java code to try to create my response:
public byte[] createResponse(InputStream inStream) {
    System.out.println("Entered createResponse");
    byte[] response = null;
    BufferedReader in = null;

    try {
        System.out.println("Creating buffered reader");
        in = new BufferedReader(new InputStreamReader(inStream, "UTF-8"));

        String filename = null;
        System.out.println("Reading file");
        String line = in.readLine(); //the HTTP request
        String queryString = null;
        System.out.println("Received: " + line);
        if (line != null && !line.trim().equals("")) {
            String[] splitRequest = line.split(" ");
            System.out.println(splitRequest[0]);
            System.out.println(splitRequest[1]);
    URL url = new URL("http://localhost/" + splitRequest[1]);
            filename = url.getPath();
            if (filename.indexOf('/') == 0) {
                filename = filename.substring(2);
            }

            System.out.println(filename);
            queryString = url.getQuery();
            System.out.println(queryString);

        }

        System.out.println("FINISHED REQUEST, STARING RESPONSE\n");

        if (filename == null) {
            response = "<html>Illegal request: no GET</html>".getBytes();
        } else {            
            File file = new File(filename);
            if (!file.exists()) {
                response = ("<html>File not found: " +
                filename + "</html>").getBytes();
            } else {
        response = captureOutput(Runtime.getRuntime().exec(filename, new String[]{queryString}));
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
        response = ("<html>ERROR: " + e.getMessage() + "</html").getBytes();
    }
    System.out.println("RESPONSE GENERATED!");
return response;
}

private byte[] captureOutput(Process p) throws IOException {
    System.out.println("Entering captureOutput");
    BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line;
    StringBuilder builder = new StringBuilder();
    while ((line = input.readLine()) != null) {
        builder.append(line);
    }
    System.out.println(builder.toString());
    return builder.toString().getBytes();
}


And here is my CGI script (written in C++):
#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

int main() {

    char* rawQueryString = getenv("QUERY_STRING");
    string queryString(rawQueryString);
    string delimiter = "&";
    string token;
    size_t position = 0;

    cout << "content-Type:text/html\n\n";
    cout << "<html><body><form method='get'>\n" << endl;
    cout << "<font color='pink'><h1>" << endl;
    if(queryString.length() > 0) {
        while ((position = queryString.find(delimiter)) != string::npos) {
            token = queryString.substr(0, position);
            cout << token << endl;
            queryString.erase(0, position + delimiter.length());
        }
        cout << queryString << endl;
    } else {
        cout << "There was not a query string" << endl;
    }

    cout << "</h1></font>\n" << endl;
    cout << "</form></body></html>\n" << endl;
    return 0;

}


For example, if I enter the url http://localhost:808...=pink&arg2=jazz in my browser, I would like to see both a page in my browser as well as a response to Java. Could anyone tell me what am I doing wrong?

Is This A Good Question/Topic? 0
  • +

Replies To: CGI - page not displaying, no output from script

#2 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 369
  • View blog
  • Posts: 1,323
  • Joined: 15-January 14

Re: CGI - page not displaying, no output from script

Posted 11 February 2014 - 02:55 PM

You pull up the page in your browser and it's blank? Is the web server configured to execute that program properly? If you view the source in the browser do you see HTML tags?
Was This Post Helpful? 0
  • +
  • -

#3 PinkJazzX  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 12-October 11

Re: CGI - page not displaying, no output from script

Posted 11 February 2014 - 05:41 PM

View PostArtificialSoldier, on 11 February 2014 - 02:55 PM, said:

You pull up the page in your browser and it's blank? Is the web server configured to execute that program properly? If you view the source in the browser do you see HTML tags?


Nope, I get a "Page Cannot Be Displayed" error, and don't see my source at all.
Was This Post Helpful? 0
  • +
  • -

#4 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 369
  • View blog
  • Posts: 1,323
  • Joined: 15-January 14

Re: CGI - page not displaying, no output from script

Posted 12 February 2014 - 09:52 AM

That sounds like there's an error on the web server, you'll probably need to check the web server's logs to find out information about the error.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1