http://www.perlmonks.org?node_id=1045548

stenasc has asked for the wisdom of the Perl Monks concerning the following question:

I know, I know, this is a perl forum and my question reards C. I should be ex-communicated, but hear me out. My perl cgi sends back a text response "Hello". Content type is plain/text. Is it possible for a C program do decode this without requiring a browser. I receive the text "Download" when I post to the cgi?

Replies are listed 'Best First'.
Re: Decode perl cgi response in C
by davido (Cardinal) on Jul 22, 2013 at 01:06 UTC

    cURL and libcurl are C-based projects. curl will happily fetch plain text given a URL. Firefox is written mostly in C as well. Perl is written in C, and obviously is up to the task. These are examples of C projects that can send requests to a webserver, listen on a port for a response, and handle it when it comes in. Plain text is probably the easiest response to handle.

    You don't have to look very far to find C programs interacting with webservers. Is that really what you were asking?


    Dave

      Yes, I have write some C TCP/UDP web clients and servers in the past and I don't have an issue there. What I have is an embedded system, that needs to process the test output from a cgi file. I hust need a little info on the way to decode this data. I send a post command to the cgi file on the server and wait for a response. Instead of the desired test response, which should be "Hello", I receive the text responses "Download" and "OK". I will read up on cURL to find out the details on the ports so I can decode the text.

        I get the feeling that you want help on the HTTP protocol. If you don't have (or can't use) any libraries to help, a minimal method would be to open a TCP connection and send a request message, which consists of the following:
        • A request line, for example GET /images/logo.png HTTP/1.1, which requests a resource called /images/logo.png from the server.
        • Request Headers, such as Accept-Language: en
        • An empty line.
        • An optional message body.
        The request line and headers must all end with <CR><LF> (that is, a carriage return character followed by a line feed character). The empty line must consist of only <CR><LF> and no other whitespace. In the HTTP/1.1 protocol, all headers except Host are optional (but in practice, it's also optional if there's only one name associated with the IP address). A request line containing only the path name is accepted by servers to maintain compatibility with HTTP clients before the HTTP/1.0 specification.

        You will then be able to read the response message, which consists of the following:

        • A Status-Line (for example HTTP/1.1 200 OK, which indicates that the client's request succeeded)
        • Zero of more Response Headers, such as Content-Type
        • An empty line
        • An optional message body
        The Status-Line and headers must all end with <CR><LF> (a carriage return followed by a line feed). The empty line must consist of only <CR><LF> and no other whitespace.

        On my computer, entering the command "telnet www.perlmonks.org 80" returns the message "Connecting To www.perlmonks.org...". If I then enter the command "GET / HTTP/1.1" and hit the <Enter> key twice, I get the following:

        HTTP/1.1 200 OK Via: 1.1 ETC-USRPXY-02 Connection: Keep-Alive Proxy-Connection: Keep-Alive Content-Length: 47589 Date: Wed, 24 Jul 2013 14:08:33 GMT Age: 74 Content-Type: text/html ETag: "b9e5-4e24268fd6b00" Server: Apache/2.2.24 Accept-Ranges: bytes Last-Modified: Wed, 24 Jul 2013 14:06:04 GMT <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head>
        ... followed by roughly 47 KB of other data. You can experiment by using telnet to connect to your server and see what you get back.
Re: Decode perl cgi response in C
by chromatic (Archbishop) on Jul 22, 2013 at 00:15 UTC
    Is it possible for a C program do decode this without requiring a browser.

    Yes, whether that C program understands HTTP itself or gets invoked by something which understands C.

    I receive the text "Download" when I post to the cgi?

    That's a vague statement. What do you expect to happen? Can you post your code?

Re: Decode perl cgi response in C
by rjt (Curate) on Jul 22, 2013 at 00:21 UTC
    Is it possible for a C program do decode this without requiring a browser.

    Yes.

    However, beyond that, I really have no idea what you're asking. I gather you have a CGI on a web server, and you seem to be asking how to process its output on the client side. For this question the CGI may as well be written in GW-BASIC; its language is irrelevant.

    What, specifically, do you need your C program to do? Do you need it to actually open a socket and communicate with the server, or only to parse the response text received some other way (how)? Unless there's more to this question, this has nothing at all to do with Perl.

Re: Decode perl cgi response in C
by ww (Archbishop) on Jul 22, 2013 at 01:08 UTC

    By 'receive "Download" when I post to the cgi.... ' do you -- by any chance -- mean that when you do some sort of submit that you are prompted to "Download" something -- as, for example, your cgi script?

    If so, you may have a permissions problem or a mis-configured Apache or cgi-bin.

    If I've misconstrued your question or the logic needed to answer it, I offer my apologies to all those electrons which were inconvenienced by the creation of this post.

      Yes...I think what it is telling me is to download a small file containing the text response. I tried an example in firefox and I got a prompt...Do you want to download test2.cgi? When I downloaded the file, it contained the desired text. "Hello". With my embedded system, I don't want to download a file...I just want to read the text it contains. Is that possible? I have a small perl client that just prints the response, so I would like to replicate this in C.

        Maybe you want to learn about "sockets"? I assume that Unix Network Programming still is the authoritative book about this.

        Basically, a "socket" is like a "file", except you can't seek on it, and it goes over the network. From C, you can read/write the socket and you only need to know about the protocol that is spoken over that socket.

        The basic idea is that "download data" vs. "read data" is only a frontend issue and exists only for users, not for programs.

Re: Decode perl cgi response in C
by DrHyde (Prior) on Jul 25, 2013 at 10:02 UTC
    Is it possible for a C program do decode this without requiring a browser?

    Your question could be rephrased as "Is it possible for one program to do part of the same job that another program does?"

    the answer is "yes".