Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何使OpenSSL使用http而不是https?_C++_Http_Ssl_Https_Openssl - Fatal编程技术网

C++ 如何使OpenSSL使用http而不是https?

C++ 如何使OpenSSL使用http而不是https?,c++,http,ssl,https,openssl,C++,Http,Ssl,Https,Openssl,所以我想要的很简单——我喜欢openSSL api。我找到了一些学习它的方法。我对服务器创建的东西还很陌生。我想知道-如何使OpenSSL与简单的http而不是https一起工作?我的意思是,我想提供相同的服务,能够在需要时跳转到https,但没有http的保护 我的意思是仅仅说 SSLServer server("cert", "pkey", 1420); // Set the thread function. server.SetPthread_F(conn_thread);

所以我想要的很简单——我喜欢openSSL api。我找到了一些学习它的方法。我对服务器创建的东西还很陌生。我想知道-如何使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成本化的类似物将是非常容易的

因此,如果我们看一看例子:

#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;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括“sslserver.h”
#定义回复“Metalshell.com OpenSSL服务器”
#定义最大数据包大小1024
//在建立新连接时调用。
void*conn_线程(void*ssl){
int fd=SSL\u get\u fd((SSL*)SSL);
如果(SSL_接受((SSL*)SSL)==-1){
错误打印错误fp(stderr);
}否则{
char-cipdesc[128];
SSL_CIPHER*sslciph=SSL_get_current_CIPHER((SSL*)SSL);

coutHTTPS是带有SSL的简单HTTP(其实现是OpenSSL的要点)。HTTPS中的S代表安全


当您不需要SSL时,不要使用OpenSSL API。

您所引用的代码是非阻塞TCP服务器+SSL的一个简单实现。因此,您需要做的是从代码中剥离OpenSSL,并且您有一个非阻塞TCP服务器的简单实现。请注意,这与真正的HTTP服务器相去甚远-它不执行请求parsing(这可能很重要),并以预定义的响应进行响应。因此,如果您需要HTTP/HTTPS服务器,则需要搜索相应的第三方库或代码。

如果您在第5章中查看(我认为),他们将通过一系列步骤构建一个简单的SSL服务器。第一步使用OpenSSL API执行明文(非SSL)网络I/O,这正是您所要求的。

只有当客户端和服务器端应用程序都“感知”时,HTTPS才会起作用大多数应用程序因此具有独立的SSL和非SSL通信入口点,例如HTTP通常位于端口80上,而HTTPS将位于端口443上。如果客户端在端口80上启动事务,并且如果服务器端应用程序确定必须保护exchange的其余部分,则“重定向”客户端应用程序以在端口443上进行交易。例如,如果您访问交易是从端口80上的通信开始的,但是由于此应用程序要求您安全登录,它会重定向您的浏览器以打开到端口443上另一个url的新连接


您也可以这样做,是的,当事务在SSL中发生时,您也可以指示客户端在端口不安全的端口80上开始事务处理。

这听起来像是ServerFault的问题。一般来说,我喜欢openSSL的服务创建api-简单http服务是否有类似的库或设置保护的方法在需要时,使用OpenSSL的NANI?这将足以让我在跨平台TCP套接字包装器上拥有简单的TCP服务器,因此问题演变成如何将TCP服务器功能从该示例代码中剥离。@ Kabumbus在谷歌搜索“简单HTTP服务器C++代码”中揭示了一对[Re]。可用的服务器,无需从您使用的示例中剥离openssl即可满足您的需要。这里的重点是使用openssl实现未来快速跳转到https world=)@Kabumbus我看不到openssl支持直通模式(这将允许您只打开和关闭SSL)。请尝试检查其他HTTP服务器代码示例之一,可能它们提供了更多有关SSL的灵活性