簡體   English   中英

C / SSL / JQuery.ajax()客戶端->服務器連接重置,但發送了1個字節

[英]C/SSL/JQuery.ajax() client -> server connection reset, but 1 byte sent

版本信息

  • 庫:

    • libgnutls-openssl.so.27(libc6,x86-64)
    • libcrypto.so.1.0.0(libc6,x86-64)
  • GCC版本:gcc(Ubuntu / Linaro 4.8.1-10ubuntu9)4.8.1

  • 操作系統( uname -voir ):3.11.0-12-generic#19-Ubuntu SMP Wed Oct 9 16:20:46 UTC 2013 x86_64 GNU / Linux
  • 瀏覽器:Google Chrome版本39.0.2171.71(64位)

腳本

通過PHP打開套接字時,我可以連接並將數據發送到C SSL服務器實例並接收響應,而所有這些都沒有問題。 但是,當我嘗試從客戶端使用JQuery AJAX進行連接時,該連接被建立,立即關閉,重新打開,最后服務器接收到一個字節“ G”,大概是GET請求的第一個字母。

結果是:

GET https:// localhost:2343 /?keyReq = 961ee53a75eef2e2 net :: ERR_CONNECTION_RESET

在Chrome的控制台中顯示。

思考

我不想太快地責怪客戶端,但是我可以使用套接字進行連接。 問題是,我的服務器需要同時處理套接字和HTTPS請求。

我也可能寫了SSL接受並以某種方式讀取了錯誤,從而使Web客戶端無法應對。

對於任何人可以借給我的正確建議,我將不勝感激。

ssl_server.c(連接位。)

void datactl( SSL *ssl ) { 
/* Serve the connection - threadable */

    char buf[1024];
    char reply[1024];
    int sd, bytes, rtn;

    if( SSL_accept( ssl ) == FAIL ) {                   /* do SSL-protocol accept */
        ERR_print_errors_fp( stderr );
    }   
    else {
        crtprint( ssl );                                /* get any certificates   */
        bytes = SSL_read( ssl, buf, sizeof( buf ) );    /* get request            */
        if( bytes > 0 ) { 
            buf[ bytes ] = 0;
            char tmp[1024];
            printf("data: '%s' (%d bytes)", buf, bytes );
            procdata( buf );                            /* process data           */
            getres( buf, reply );                       /* construct reply        */
            SSL_write( ssl, reply,
                            strlen( reply );            /* send reply             */
        }
        else
            ERR_print_errors_fp( stderr );
    }   
    sd = SSL_get_fd( ssl );                             /* get socket connection  */
    SSL_free( ssl );                                    /* release SSL state      */
    close( sd );                                        /* close connection       */
}

/* main - create SSL socket server. */
int main( int argc, char *argv[] ) { 

    SSL_CTX *ctx;
    int server;
    int len;
    int addrlen;
    char cwd[1024];
    char crt[1024];
    char key[1024];

    getcwd( cwd, sizeof( cwd ) );

    strcpy( crt, cwd );
    strcpy( key, cwd );
    strcat( crt, "/servers/certs/server.crt" );
    strcat( key, "/servers/certs/server.key" );

    ctx = initsrvctx(); /* initialize SSL. */
    getcrts( ctx, crt, key ); /* load certs.           */
    server = startsck( PORTNUM ); /* create server socket. */
/* (PORTNUM is defined at compile time by using -DPORTNUM=#### */

    while( 1 ) { 
        struct sockaddr_in serv;
        int len = sizeof( serv );
        SSL *ssl;

        int client = accept(
            server,
            ( struct sockaddr * ) &serv,
            &len
        ); /* accept connection as usual. */

        ssl = SSL_new( ctx ); /* get new SSL state with context */
        SSL_set_fd( ssl, client ); /* set connection socket to SSL state */
        datactl( ssl ); /* service connection */
    }   
    close( server ); /* close server socket */
    SSL_CTX_free( ctx ); /* release context */
}

test.php的

<?php require('./inc/head.php'); ?>
<script type="text/javascript">
function keyRequest() {
    $.ajax({
        type: 'GET',
        url: 'https://localhost:2343',
        data: { hello: "hello", world: "world" }
    });
}

keyRequest();
</script>

輸出(這里的錯誤是自定義的。)

2015-02-04 15:58:14 [OPEN]  | Connection opened with client. (127.0.0.1)
2015-02-04 15:58:14 [CLOSE] | Connection with client closed.
2015-02-04 15:58:14 [OPEN]  | Connection opened with client. (127.0.0.1)
2015-02-04 15:58:14 [DEBUG] | data: 'G' (1 bytes)
2015-02-04 15:58:14 [DEBUG] | Starting data processing.
2015-02-04 15:58:14 [ERR]   | Malformed or invalid data. Code: 0x10a.
2015-02-04 15:58:14 [ERR]   | Malformed or invalid data. Code: 0x10a.
2015-02-04 15:58:14 [CLOSE] | Connection with client closed.

C中的緩沖區很小,您應該閱讀直到獲得流的末尾-這通常是在請求的“ Content-Length”標頭中指定的。 我建議您閱讀HTTP協議標准,此后,我確信您可以輕松找到問題的解決方案,並且連接斷開問題也將得到解決。

我設法解決了我的問題。 在指定要使用的SSL_method ,我使用SSLv3_server_method()而不是SSLv23_server_method() 更改此設置后,讀取緩沖區沒有問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM