【发布时间】:2011-01-09 16:37:12
【问题描述】:
所以我想要的很简单——我喜欢 openSSL api。我找到了一些simple code to begin with 来学习它。我对服务器创建的东西很陌生。我想知道 - 如何让 OpenSSL 使用简单的 http 而不是 https?我的意思是我想提供同样的服务,能够在我需要的时候跳转到 https 但没有保护它的 http 版本。
我的意思是说出来真是太棒了
SSLServer server("cert", "pkey", 1420);
// Set the thread function.
server.SetPthread_F(conn_thread);
我希望我可以为不受保护的 http 服务创建做同样的事情。
在我明白了一些重要的答案后,我将编辑主要问题:
如何只保留/使用 OpenSSL 库的非阻塞 TCP 服务器部分?主要目标将是一个跨平台的小型且易于使用的 TCP 服务器,在该服务器上可以轻松实现 http 和 http costumized 类似物
所以如果我们看一下例子:
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "sslserver.h"
#define REPLY "<html><body>Metalshell.com OpenSSL Server</body></html>"
#define MAX_PACKET_SIZE 1024
// Called when a new connection is made.
void *conn_thread(void *ssl) {
int fd = SSL_get_fd((SSL *)ssl);
if(SSL_accept((SSL *)ssl) == -1) {
ERR_print_errors_fp(stderr);
} else {
char cipdesc[128];
SSL_CIPHER *sslciph = SSL_get_current_cipher((SSL *)ssl);
cout << "Encryption Description:\n";
cout << SSL_CIPHER_description(sslciph, cipdesc, sizeof(cipdesc)) << endl;
char buff[MAX_PACKET_SIZE];
// Wait for data to be sent.
int bytes = SSL_read((SSL *)ssl, buff, sizeof(buff));
buff[bytes] = '\0';
// Show the browser request.
cout << "Recieved: \n" << buff << endl;
// Send the html reply.
SSL_write((SSL *)ssl, REPLY, strlen(REPLY));
}
// Tell the client we are closing the connection.
SSL_shutdown((SSL *)ssl);
// We do not wait for a reply, just clear everything.
SSL_free((SSL *)ssl);
close(fd);
cout << "Connection Closed\n";
cout << "---------------------------------------------\n";
pthread_exit(NULL);
}
int main() {
SSLServer server("cert", "pkey", 1420);
// Set the thread function.
server.SetPthread_F(conn_thread);
while(1) {
/* Wait for 10 seconds, and if no one trys
* to connect return back. This allows us to do
* other things while waiting.
*/
server.CheckClients(10);
}
return 0;
}
我们的服务器应该改变什么来接受所有连接,而不仅仅是 ssl 连接(如果可能,cout 完整请求)并向它们发送回复?
【问题讨论】:
-
这听起来像是 ServerFault 的问题。
标签: c++ http ssl https openssl