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
- OS (
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 accept 并以某种方式读取错误,以至于 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" header 中指定。我建议你阅读 HTTP 协议标准,之后我相信你会很容易找到问题的解决方案,连接断开问题也将得到解决。
我设法解决了我的问题。在指定要使用的 SSL_method
时,我使用 SSLv3_server_method()
而不是 SSLv23_server_method()
。改了这个之后,读到buffer就没有问题了
版本信息
库:
- 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
- OS (
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 accept 并以某种方式读取错误,以至于 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" header 中指定。我建议你阅读 HTTP 协议标准,之后我相信你会很容易找到问题的解决方案,连接断开问题也将得到解决。
我设法解决了我的问题。在指定要使用的 SSL_method
时,我使用 SSLv3_server_method()
而不是 SSLv23_server_method()
。改了这个之后,读到buffer就没有问题了