Клиент TLS в MSVC ++ для локального хоста: Порт - PullRequest
0 голосов
/ 06 ноября 2019

У меня есть локальный сервер с REST API, работающим на https://localhost:61000,, на который я могу отправить следующий запрос Curl:

curl -X POST "https://localhost:61000/users/logout" -H "accept: */*" -H "Locale: en"

, и он вернет мне следующий ответ

enter image description here

В MSVC ++ с использованием библиотеки WinSock и OpenSSL я могу получить код ответа 200 из google.com через порт 443,но когда я пытаюсь использовать localhost на порту 61000, я всегда получаю ответ HTTP/1.1 400 Bad Request

// Client

int main(int argc, char* argv[])
{
    WSADATA wsadata;
    if ((WSAStartup(MAKEWORD(2, 0), &wsadata)) == 0) {
        cout << "-WSAStartup Initialized." << endl;
        struct addrinfo hints, * res;
        int sockfd;

        memset(&hints, 0, sizeof hints);
        hints.ai_socktype = 0;// SOCK_STREAM;
        hints.ai_protocol = 0;// IPPROTO_TCP;
        hints.ai_family = AF_UNSPEC;

        if (getaddrinfo("localhost", "61000", &hints, &res) != 0) {
            cout << "-getaddrinfo unsuccessful." << WSAGetLastError() << endl;
        }

        if ((sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) == INVALID_SOCKET) {
            cout << "-Unable to create socket." << WSAGetLastError() << endl;
        }

        if ((connect(sockfd, res->ai_addr, res->ai_addrlen)) != SOCKET_ERROR) {
            cout << "-Connection Established." << endl;
        }

        cout << "-Client connected to: " << res->ai_addr << endl;

        SSL_library_init();
        SSLeay_add_ssl_algorithms();
        SSL_load_error_strings();

        const SSL_METHOD* meth = TLSv1_2_client_method();
        SSL_CTX* ctx = SSL_CTX_new(meth);

        ssl = SSL_new(ctx);
        if (!ssl) {
            printf("Error creating SSL.\n");
            log_ssl();
            return -1;
        }
        sock = SSL_get_fd(ssl);
        SSL_set_fd(ssl, sockfd);
        int err = SSL_connect(ssl);
        if (err <= 0) {
            printf("Error creating SSL connection.  err=%x\n", err);
            log_ssl();
            fflush(stdout);
            return -1;
        }
        printf("SSL connection using %s\n", SSL_get_cipher(ssl));

        const char* request = "GET /users HTTPS/1.1\r\n\r\n";
     //SendPacket
        int len = SSL_write(ssl, request, strlen(request));
        if (len < 0) {
            int err = SSL_get_error(ssl, len);
            switch (err) {
            case SSL_ERROR_WANT_WRITE:
                return 0;
            case SSL_ERROR_WANT_READ:
                return 0;
            case SSL_ERROR_ZERO_RETURN:
            case SSL_ERROR_SYSCALL:
            case SSL_ERROR_SSL:
            default:
                return -1;
            }
        }

        //RecvPacket
        len = 100;
        char buf[20000];
        do {
            len = SSL_read(ssl, buf, 100);
            buf[len] = 0;
            printf(buf);
        } while (len > 0);
        if (len < 0) {
            int err = SSL_get_error(ssl, len);
            if (err == SSL_ERROR_WANT_READ)
                return 0;
            if (err == SSL_ERROR_WANT_WRITE)
                return 0;
            if (err == SSL_ERROR_ZERO_RETURN || err == SSL_ERROR_SYSCALL || err == SSL_ERROR_SSL)
                return -1;
        }
    }
    else {
        cout << "-WSAStartup Initialization failed." << endl;
        if (WSACleanup() != 0) {
            cout << "-WSACleanup Successful." << endl;
        }
        else {
            cout << "-WSACleanup Failed." << endl;
        }
    }
    return 0;
}

: enter image description here

Предполагая, что (HTTP) 400 BadКод состояния ответа на запрос указывает, что сервер не может или не будет обрабатывать запрос из-за того, что воспринимается как ошибка клиента, но я не могу понять, как правильно создать запрос.

...