Why doesn't it work?

This is a discussion on Why doesn't it work? within the C++ Programming forums, part of the General Programming Boards category; This is the code of the program that should post data to a php script: Code: #define _DEBUG_PRINT(X) /* X ...

  1. #1
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318

    Why doesn't it work?

    This is the code of the program that should post data to a php script:
    Code:
    #define _DEBUG_PRINT(X)   /* X */
    #include <iostream>
    #include <string>
    #include <stdlib.h>
    #include <assert.h>
    #include <Winsock2.h>
    
    
    #define SEND_RQ(MSG) \
                    /*cout<<send_str;*/ \
      send(sock,MSG,strlen(MSG),0);
    
    
    using namespace std;
    //<exe> hostname api parameters
    int request (char* hostname, char* api, char* parameters, string& message)
    {
    
    	#ifdef WIN_OS
    	{
    		WSADATA	WsaData;
    		WSAStartup (0x0101, &WsaData);
    	}
    	#endif
    
        sockaddr_in       sin;
        int sock = socket (AF_INET, SOCK_STREAM, 0);
        if (sock == -1) {
    		return -100;
    	}
        sin.sin_family = AF_INET;
        sin.sin_port = htons( (unsigned short)80);
    
        struct hostent * host_addr = gethostbyname(hostname);
        if(host_addr==NULL) {
          cout<<"Unable to locate host"<<endl;
          return -103;
        }
        sin.sin_addr.s_addr = *((int*)*host_addr->h_addr_list) ;
        cout<<"Port :"<<sin.sin_port<<", Address : "<< sin.sin_addr.s_addr<<endl;
    
        if( connect (sock,(const struct sockaddr *)&sin, sizeof(sockaddr_in) ) == -1 ) {
         cout<<"connect failed"<<endl;
         return -101;
        }
    
     string send_str;
    
     SEND_RQ("POST ");
     SEND_RQ(api);
     SEND_RQ(" HTTP/1.0\r\n");
     SEND_RQ("Accept: */*\r\n");
     SEND_RQ("User-Agent: Mozilla/4.0\r\n");
    
     char content_header[100];
     sprintf(content_header,"Content-Length: %d\r\n",strlen(parameters));
     SEND_RQ(content_header);
     SEND_RQ("Accept-Language: en-us\r\n");
     SEND_RQ("Accept-Encoding: gzip, deflate\r\n");
     SEND_RQ("Host: ");
     SEND_RQ("hostname");
     SEND_RQ("\r\n");
     SEND_RQ("Content-Type: application/x-www-form-urlencoded\r\n");
     
     //If you need to send a basic authorization
     //string Auth        = "username:password";
     //Figureout a way to encode test into base64 !
     //string AuthInfo    = base64_encode(reinterpret_cast<const unsigned char*>(Auth.c_str()),Auth.length());
     //string sPassReq    = "Authorization: Basic " + AuthInfo;
     //SEND_RQ(sPassReq.c_str());
    
     SEND_RQ("\r\n");
     SEND_RQ("\r\n");
     SEND_RQ(parameters);
     SEND_RQ("\r\n");
    
     _DEBUG_PRINT(cout<<"####HEADER####"<<endl);
     char c1[1];
     int l,line_length;
     bool loop = true;
     bool bHeader = false;
    
     while(loop) {
       l = recv(sock, c1, 1, 0);
       if(l<0) loop = false;
       if(c1[0]=='\n') {
           if(line_length == 0) loop = false;
    
           line_length = 0;
           if(message.find("200") != string::npos)
    	       bHeader = true;
    
       }
       else if(c1[0]!='\r') line_length++;
       _DEBUG_PRINT( cout<<c1[0]);
       message += c1[0];
     }
    
     message="";
     if(bHeader) {
    
         _DEBUG_PRINT( cout<<"####BODY####"<<endl) ;
         char p[1024];
         while((l = recv(sock,p,1023,0)) > 0)  {
             _DEBUG_PRINT( cout.write(p,l)) ;
    	     p[l] = '\0';
    	     message += p;
         }
    
         _DEBUG_PRINT( cout << message.c_str());
     } else {
    	 return -102;
     }
    
    
     #ifdef WIN_OS
       WSACleanup( );
     #endif
    
     return 0;
    }
    
    
    int main(){
      std::string message;
      request ("http://maxorator.farvista.net", "/raab.php", "some=this should be received", message);
      cin.get();
    
    }
    Php file:
    Code:
    <?php
    $handle=fopen("jara.txt","w+");
    $HTTP_POST_VARS['some'].="!";
    fwrite($handle,$_POST['some']);
    fclose($handle);
    ?>
    I don't understand, why doesn't it work. I added $HTTP_POST_VARS['some'].="!"; because I wanted to test, does it execute the php script. But it doesn't even execute that php script on the server. I tried to use it with the full path:
    Code:
    request ("http://maxorator.farvista.net", "/home/www/maxorator.farvista.net/raab.php", "some=this should be received", message);
    Can anyone help me with this?

  2. #2
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    what errors are you getting? Are you sure that WIN_OS is defined? If you are including winsock2.h, you don't need to check if you are using windows in the code.
    Also, I think you have 1 too many SEND_RQ("\r\n"); After the HTTP header you need two returns, but you already put 1 return after the last line.

  3. #3
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    I didn't get any errors. It just doesn't return anything to message and doesn't cout any error, just doesn't execute the php script on the server.

  4. #4
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    try removing the #ifdefs and just execute the WSAStartup code always. You don't need this if you include winsock2.h directly, also, try changing the to winsock.h, you aren't using anything winsock2 specific. Also, put some "cout" statements wherever your return, such as when the return value of socket is invalid. Then run it and see what output you get.

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    The first thing I notice is that you are never checking the return value of send. This can be very very very bad.
    RETURN VALUE
    The calls return the number of characters sent, or -1 if
    an error occurred.
    So what happens if it can only send 3 characters instead of the 10 you asked it to send?

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    BTW since you are using C++ don't use #define to create fake functions. Use a real function and inline:

    Example:
    Code:
    inline void SEND_RQ(const char* MSG)
    {
      send(sock,MSG,strlen(MSG),0);
    }
    which will let you easily avoid the problem I mentioned before:
    Code:
    inline bool SEND_RQ(const char* MSG)
    {
      int len = strlen(MSG);
      int sent = 0;
      while ( sent < len )
      {
        int ret = send(sock,MSG+sent,len - sent,0);
        if ( ret == -1 ) return false;
        sent += ret;
      }
      return true;
    }
    Of course you still have to find a way to pass the socket in but I'll leave that to you

  7. #7
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Code:
    #define _DEBUG_PRINT(X)   /* X */
    #include <iostream>
    #include <string>
    #include <stdlib.h>
    #include <assert.h>
    #include <winsock.h>
    #include <windows.h>
    int sock;
    using namespace std;
    inline int SEND_RQ(char* happy);
    //<exe> hostname api parameters
    int request (char* hostname, char* api, char* parameters, string& message)
    {
    
    		WSADATA	WsaData;
    		WSAStartup (0x0101, &WsaData);
    
        sockaddr_in       sin;
        sock = socket (AF_INET, SOCK_STREAM, 0);
        if (sock == -1) {
    		return -100;
    	}
        sin.sin_family = AF_INET;
        sin.sin_port = htons( (unsigned short)80);
    
        struct hostent * host_addr = gethostbyname(hostname);
        if(host_addr==NULL) {
          cout<<"Unable to locate host"<<endl;
          return -103;
        }
        sin.sin_addr.s_addr = *((int*)*host_addr->h_addr_list) ;
        cout<<"Port :"<<sin.sin_port<<", Address : "<< sin.sin_addr.s_addr<<endl;
    
        if( connect (sock,(const struct sockaddr *)&sin, sizeof(sockaddr_in) ) == -1 ) {
         cout<<"connect failed"<<endl;
         return -101;
        }
     string send_str;
    
     SEND_RQ("POST ");
     SEND_RQ(api);
     SEND_RQ(" HTTP/1.0\r\n");
     SEND_RQ("Accept: */*\r\n");
     SEND_RQ("User-Agent: Mozilla/4.0\r\n");
    
     char content_header[100];
     sprintf(content_header,"Content-Length: %d\r\n",strlen(parameters));
     SEND_RQ(content_header);
     SEND_RQ("Accept-Language: en-us\r\n");
     SEND_RQ("Accept-Encoding: gzip, deflate\r\n");
     SEND_RQ("Host: ");
     SEND_RQ("hostname");
     SEND_RQ("\r\n");
     SEND_RQ("Content-Type: application/x-www-form-urlencoded\r\n");
     
     //If you need to send a basic authorization
     //string Auth        = "username:password";
     //Figureout a way to encode test into base64 !
     //string AuthInfo    = base64_encode(reinterpret_cast<const unsigned char*>(Auth.c_str()),Auth.length());
     //string sPassReq    = "Authorization: Basic " + AuthInfo;
     //SEND_RQ(sPassReq.c_str());
    
     SEND_RQ("\r\n");
     SEND_RQ("\r\n");
     SEND_RQ(parameters);
     SEND_RQ("\r\n");
    
     cout<<"####HEADER####"<<endl;
     char c1[1];
     int l,line_length;
     bool loop = true;
     bool bHeader = false;
    
     while(loop) {
       l = recv(sock, c1, 1, 0);
       if(l<0) loop = false;
       if(c1[0]=='\n') {
           if(line_length == 0) loop = false;
    
           line_length = 0;
           if(message.find("200") != string::npos)
    	       bHeader = true;
    
       }
       else if(c1[0]!='\r') line_length++;
       cout<<c1[0];
       message += c1[0];
     }
     message="";
     if(bHeader) {
    
         cout<<"####BODY####"<<endl;
         char p[1024];
         while((l = recv(sock,p,1023,0)) > 0)  {
             cout.write(p,l);
    	     p[l] = '\0';
    	     message += p;
         }
    
         cout << message.c_str();
     } else {
    	 return -102;
     }
    
    
       WSACleanup( );
    
     return 0;
    }
    
    inline int SEND_RQ(char* happy){
             send(sock,happy,strlen(happy),0);
             return 0;         
    }
    int main(){
      std::string message;
      request ("maxorator.farvista.net", "/home/www/maxorator.farvista.net/raab.php", "", message);
      cout<<message;
      cin.get();
      // message contains response!
    
    }
    With this i always get reply:
    Code:
    Port :20480, Address : 1264738881
    ####HEADER####
    HTTP/1.1 302 Found
    
    Date: Wed, 12 Oct 2005 12:19:12 GMT
    
    Server: Apache/1.3.33 (Unix) mod_ssl/2.8.22 OpenSSL/0.9.7d VDB/1.1.1
    
    Location: http://www.farvista.net
    
    Connection: close
    
    Content-Type: text/html; charset=iso-8859-1
    
    
    
    ####BODY####
    Č<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
    <HTML><HEAD>
    <TITLE>302 Found</TITLE>
    </HEAD><BODY>
    <H1>Found</H1>
    The document has moved <A HREF="http://www.farvista.net">here</A>.<P>
    <P>Additionally, a 302 Found
    error was encountered while trying to use an ErrorDocument to handle the request.
    <HR>
    <ADDRESS>Apache/1.3.33 Server at _default_ Port 80</ADDRESS>
    </BODY></HTML>

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
    10.3.3 302 Found

    The requested resource resides temporarily under a different URI. Since the redirection might be altered on occasion, the client SHOULD continue to use the Request-URI for future requests. This response is only cacheable if indicated by a Cache-Control or Expires header field.

    The temporary URI SHOULD be given by the Location field in the response. Unless the request method was HEAD, the entity of the response SHOULD contain a short hypertext note with a hyperlink to the new URI(s).

    If the 302 status code is received in response to a request other than GET or HEAD, the user agent MUST NOT automatically redirect the request unless it can be confirmed by the user, since this might change the conditions under which the request was issued.

    Note: RFC 1945 and RFC 2068 specify that the client is not allowed
    to change the method on the redirected request. However, most
    existing user agent implementations treat 302 as if it were a 303
    response, performing a GET on the Location field-value regardless
    of the original request method. The status codes 303 and 307 have
    been added for servers that wish to make unambiguously clear which
    kind of reaction is expected of the client.
    Welcome to the fun world of the html protocol

  9. #9
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Well, what should I change then...

  10. #10
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    something looks fishy with:
    request ("maxorator.farvista.net", "/home/www/maxorator.farvista.net/raab.php", "", message);
    The actual file path is suppose to be transparent to the client. Unless of course you are doing a FTP connection and not a HTTP connection.

  11. #11
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    With ftp it displays www folder, but with the file manager on the cpanel I can see other folders like sys,tmp,logs,mail there.

  12. #12
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    But php scripts show me that the path is /home/www/maxorator.farvista.net/

  13. #13
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    You are making a HTTP connection. As such its up to the server to serve the correct document.

    If you go to http://www.cprogramming.com/index.php you have no idea where that document resides on the server.

    This is my advice:
    Get rid of the POST part and just make a connection to a normal page and echo the results. Once you have that part down then you can move on from there.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 08:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 11:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 11:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 05:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21