简体   繁体   English

C / SSL / JQuery.ajax()客户端->服务器连接重置,但发送了1个字节

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

Version Info 版本信息

  • Libs: 库:

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

  • OS ( uname -voir ): 3.11.0-12-generic #19-Ubuntu SMP Wed Oct 9 16:20:46 UTC 2013 x86_64 GNU/Linux 操作系统( uname -voir ):3.11.0-12-generic#19-Ubuntu SMP Wed Oct 9 16:20:46 UTC 2013 x86_64 GNU / Linux
  • Browser: Google Chrome Version 39.0.2171.71 (64-bit) 浏览器:Google Chrome版本39.0.2171.71(64位)

Scenario 脚本

When opening a socket via PHP, I can connect and send data to my instance of a C SSL server and receive a response, all with no issues. 通过PHP打开套接字时,我可以连接并将数据发送到C SSL服务器实例并接收响应,而所有这些都没有问题。 However, when I attempt to connect using JQuery AJAX from a client, the connection is established, immediately closed, reopened, and finally the server receives a single byte 'G', presumably the first letter of the GET request. 但是,当我尝试从客户端使用JQuery AJAX进行连接时,该连接被建立,立即关闭,重新打开,最后服务器接收到一个字节“ G”,大概是GET请求的第一个字母。

This results in: 结果是:

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

being displayed in Chrome's console. 在Chrome的控制台中显示。

Thoughts 思考

I don't want to be too quick to blame the client side, but I can connect using sockets just fine. 我不想太快地责怪客户端,但是我可以使用套接字进行连接。 Problem is, my server needs to deal with both socket and HTTPS requests. 问题是,我的服务器需要同时处理套接字和HTTPS请求。

It's also possible that I wrote the SSL accept and reads wrong somehow, in such a way that a web client can't cope. 我也可能写了SSL接受并以某种方式读取了错误,从而使Web客户端无法应对。

I would be very grateful for any advice anyone could lend me to get me going in the right direction. 对于任何人可以借给我的正确建议,我将不胜感激。

Code

ssl_server.c (The connection bits.) 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 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>

Output (The errors here are self defined.) 输出(这里的错误是自定义的。)

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.

The buffer in C is to small, you should read until you get the end of the stream - this is ussualy specified in "Content-Length" header in the request. C中的缓冲区很小,您应该阅读直到获得流的末尾-这通常是在请求的“ Content-Length”标头中指定的。 I advice you to read the HTTP Protocol Standard, afterwards I am sure that you will easily find the solution to your problem and the connect disconnect issue will also be solved. 我建议您阅读HTTP协议标准,此后,我确信您可以轻松找到问题的解决方案,并且连接断开问题也将得到解决。

I managed to fix my problem. 我设法解决了我的问题。 When specifying the SSL_method to be used, I had SSLv3_server_method() instead of SSLv23_server_method() . 在指定要使用的SSL_method ,我使用SSLv3_server_method()而不是SSLv23_server_method() After changing this, there were no problems with reading to the buffer. 更改此设置后,读取缓冲区没有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM