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

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

本文关键字:字节 连接 ajax JQuery SSL 客户端 服务器      更新时间:2023-09-26

版本信息

  • 库:

    • 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-通用 #19-Ubuntu SMP 星期三 十月 9 16:20:46 UTC 2013 x86_64 GNU/Linux
  • 浏览器: 谷歌浏览器版本 39.0.2171.71 (64 位)

场景

通过 PHP 打开套接字时,我可以连接数据并将其发送到我的 C SSL 服务器实例并接收响应,所有这些都没有问题。但是,当我尝试从客户端使用 JQuery AJAX 进行连接时,连接已建立,立即关闭,重新打开,最后服务器收到单个字节"G",大概是 GET 请求的第一个字母。

这导致:

获取 https://localhost:2343/?keyReq=961ee53a75eef2e2 网::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 */
}

测试.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 中的缓冲区很小,您应该读取直到获得流的末尾 - 这是在请求的"内容长度"标头中指定的。我建议您阅读HTTP协议标准,之后我相信您将轻松找到问题的解决方案,并且连接断开连接问题也将得到解决。

我设法解决了我的问题。在指定要使用的SSL_method时,我使用了SSLv3_server_method()而不是SSLv23_server_method()。更改此设置后,读取缓冲区没有问题。