WinInet и заголовки в ответах сервера

Тема в разделе "WASM.NETWORKS", создана пользователем Sickle, 23 дек 2006.

  1. Sickle

    Sickle New Member

    Публикаций:
    0
    Регистрация:
    11 июл 2003
    Сообщения:
    181
    Доброе... работаю с ssl соединением через функции wininet. столкнулся с такой проблемой - не могу получить заголовок ответа сервера. был бы рад работать через сокеты - но блин ssl. как решить данную проблему? также, буду благодарен за примеры работы с OpenSSL - возможно это будет лучшим решением задачи.
     
  2. n0name

    n0name New Member

    Публикаций:
    0
    Регистрация:
    5 июн 2004
    Сообщения:
    4.336
    Адрес:
    Russia
    Код (Text):
    1. /**********************************************************************
    2.  * https_client.c --- very simple HTTPS client with no error checking
    3.  *     usage: https_client servername
    4.  **********************************************************************/
    5.  
    6. #include <stdio.h>
    7. #include <memory.h>
    8. #include <errno.h>
    9. #include <sys/types.h>
    10. #include <sys/socket.h>
    11. #include <netinet/in.h>
    12. #include <arpa/inet.h>
    13. #include <netdb.h>
    14.  
    15. #include <openssl/crypto.h>
    16. #include <openssl/x509.h>
    17. #include <openssl/pem.h>
    18. #include <openssl/ssl.h>
    19. #include <openssl/err.h>
    20.  
    21. void main(int argc, char **argv)
    22. {
    23.     SSL *ssl;
    24.     SSL_CTX *ctx;
    25.     SSL_METHOD *client_method;
    26.     X509 *server_cert;
    27.     int sd,err;
    28.     char *str,*hostname,outbuf[4096],inbuf[4096],host_header[512];
    29.     struct hostent *host_entry;
    30.     struct sockaddr_in server_socket_address;
    31.     struct in_addr ip;
    32.  
    33.     /*========================================*/
    34.     /* (1) initialize SSL library */
    35.     /*========================================*/
    36.  
    37.     SSLeay_add_ssl_algorithms(  );
    38.     client_method = SSLv2_client_method(  );
    39.     SSL_load_error_strings(  );
    40.     ctx = SSL_CTX_new(client_method);
    41.  
    42.     printf("(1) SSL context initialized\n\n");
    43.  
    44.     /*=============================================*/
    45.     /* (2) convert server hostname into IP address */
    46.     /*=============================================*/
    47.  
    48.     hostname = argv[1];
    49.     host_entry = gethostbyname(hostname);
    50.     bcopy(host_entry->h_addr, &(ip.s_addr), host_entry->h_length);
    51.  
    52.     printf("(2) '%s' has IP address '%s'\n\n", hostname, inet_ntoa(ip));
    53.  
    54.     /*=================================================*/
    55.     /* (3) open a TCP connection to port 443 on server */
    56.     /*=================================================*/
    57.  
    58.     sd = socket (AF_INET, SOCK_STREAM, 0);
    59.  
    60.     memset(&server_socket_address, '\0', sizeof(server_socket_address));
    61.     server_socket_address.sin_family = AF_INET;
    62.     server_socket_address.sin_port = htons(443);
    63.     memcpy(&(server_socket_address.sin_addr.s_addr),
    64.            host_entry->h_addr, host_entry->h_length);
    65.  
    66.     err = connect(sd, (struct sockaddr*) &server_socket_address,
    67.                   sizeof(server_socket_address));
    68.     if (err < 0) { perror("can't connect to server port"); exit(1); }
    69.  
    70.     printf("(3) TCP connection open to host '%s', port %d\n\n",
    71.            hostname, server_socket_address.sin_port);
    72.  
    73.     /*========================================================*/
    74.     /* (4) initiate the SSL handshake over the TCP connection */
    75.     /*========================================================*/
    76.  
    77.     ssl = SSL_new(ctx);         /* create SSL stack endpoint */
    78.     SSL_set_fd(ssl, sd);        /* attach SSL stack to socket */
    79.     err = SSL_connect(ssl);     /* initiate SSL handshake */
    80.  
    81.     printf("(4) SSL endpoint created & handshake completed\n\n");
    82.    
    83.     /*============================================*/
    84.     /* (5) print out the negotiated cipher chosen */
    85.     /*============================================*/
    86.    
    87.     printf("(5) SSL connected with cipher: %s\n\n", SSL_get_cipher(ssl));
    88.  
    89.     /*========================================*/
    90.     /* (6) print out the server's certificate */
    91.     /*========================================*/
    92.  
    93.     server_cert = SSL_get_peer_certificate(ssl);
    94.  
    95.     printf("(6) server's certificate was received:\n\n");
    96.  
    97.     str = X509_NAME_oneline(X509_get_subject_name(server_cert), 0, 0);
    98.     printf("      subject: %s\n", str);
    99.  
    100.     str = X509_NAME_oneline(X509_get_issuer_name(server_cert), 0, 0);
    101.     printf("      issuer: %s\n\n", str);
    102.  
    103.     /* certificate verification would happen here */
    104.  
    105.     X509_free(server_cert);
    106.  
    107.     /*********************************************************/
    108.     /* (7) handshake complete --- send HTTP request over SSL */
    109.     /*********************************************************/
    110.  
    111.     sprintf(host_header,"Host: %s:443\r\n",hostname);
    112.     strcpy(outbuf,"GET / HTTP/1.0\r\n");
    113.     strcat(outbuf,host_header);
    114.     strcat(outbuf,"Connection: close\r\n");
    115.     strcat(outbuf,"\r\n");
    116.  
    117.     err = SSL_write(ssl, outbuf, strlen(outbuf));
    118.     shutdown (sd, 1); /* send EOF to server */
    119.  
    120.     printf("(7) sent HTTP request over encrypted channel:\n\n%s\n",outbuf);
    121.  
    122.     /**************************************************/
    123.     /* (8) read back HTTP response from the SSL stack */
    124.     /**************************************************/
    125.  
    126.     err = SSL_read(ssl, inbuf, sizeof(inbuf) - 1);
    127.     inbuf[err] = '\0';
    128.     printf ("(8) got back %d bytes of HTTP response:\n\n%s\n",err,inbuf);
    129.  
    130.     /************************************************/
    131.     /* (9) all done, so close connection & clean up */
    132.     /************************************************/
    133.  
    134.     SSL_shutdown(ssl);
    135.     close (sd);
    136.     SSL_free (ssl);
    137.     SSL_CTX_free (ctx);
    138.  
    139.     printf("(9) all done, cleaned up and closed connection\n\n");
    140. }
    Спёр из книжки по HTTP.
     
  3. Sickle

    Sickle New Member

    Публикаций:
    0
    Регистрация:
    11 июл 2003
    Сообщения:
    181
    n0name
    благодарю!
    а по первой части моего вопроса я выяснил - функция HttpQueryInfo позволяет получить нужные данные из заголовка.
     
  4. RuAsm

    RuAsm Виктор

    Публикаций:
    0
    Регистрация:
    16 июл 2006
    Сообщения:
    125
    Адрес:
    Спасск-D, Приморский край!
    Sickle респееект!) молодец что написал решение, долго уже ищу)
     
  5. RuAsm

    RuAsm Виктор

    Публикаций:
    0
    Регистрация:
    16 июл 2006
    Сообщения:
    125
    Адрес:
    Спасск-D, Приморский край!
    пример использования
    Код (Text):
    1. char buff[1024];
    2. DWORD dwSize=1024;
    3. HttpQueryInfo(hRequest, HTTP_QUERY_RAW_HEADERS_CRLF, buff, &dwSize, NULL);
    4. cout << buff;