aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/http/fetch/http_socket.cpp
blob: 7a5b29668ce4eccb9df5a5321d4473e9dd35048e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include "httpload.h"
#include "http_digest.h"

/************************************************************/

#ifdef USE_GNUTLS

#include <gcrypt.h>
#include <gnutls/gnutls.h>
#include <util/network/init.h> 
#include <util/network/socket.h>
#include <util/system/mutex.h>

/********************************************************/
// HTTPS handler is used as implementation of
// socketAbstractHandler for work through HTTPS protocol

class socketSecureHandler: public socketRegularHandler { 
protected: 
    bool IsValid_; 
    gnutls_session Session_; 
    gnutls_certificate_credentials Credits_; 

public: 
    socketSecureHandler(); 
    virtual ~socketSecureHandler(); 

    virtual bool Good(); 
    virtual int Connect(const TAddrList& addrs, TDuration Timeout); 
    virtual void Disconnect(); 
    virtual void shutdown(); 
    virtual bool send(const char* message, ssize_t messlen); 
    virtual bool peek(); 
    virtual ssize_t read(void* buffer, ssize_t buflen); 
};

/********************************************************/
/********************************************************/
static int gcry_pthread_mutex_init(void** priv) { 
    int err = 0;

    try {
        TMutex* lock = new TMutex;
        *priv = lock;
    } catch (...) { 
        err = -1;
    }

    return err;
}

static int gcry_pthread_mutex_destroy(void** lock) { 
    delete static_cast<TMutex*>(*lock);
 
    return 0;
}

static int gcry_pthread_mutex_lock(void** lock) { 
    static_cast<TMutex*>(*lock)->Acquire();

    return 0;
}

static int gcry_pthread_mutex_unlock(void** lock) { 
    static_cast<TMutex*>(*lock)->Release();

    return 0;
}

static struct gcry_thread_cbs gcry_threads_pthread =
    { 
        GCRY_THREAD_OPTION_PTHREAD, NULL, 
        gcry_pthread_mutex_init, gcry_pthread_mutex_destroy, 
        gcry_pthread_mutex_lock, gcry_pthread_mutex_unlock, 
        NULL, NULL, NULL, NULL, 
        NULL, NULL, NULL, NULL}; 

/********************************************************/
struct https_initor { 
    https_initor() { 
        gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread); 
        gnutls_global_init();
        InitNetworkSubSystem(); 
    }

    ~https_initor() { 
        gnutls_global_deinit();
    }
};

static https_initor _initor;

/********************************************************/
socketSecureHandler::socketSecureHandler() 
    : socketRegularHandler() 
    , IsValid_(false) 
    , Session_() 
    , Credits_() 
{
}

/********************************************************/
socketSecureHandler::~socketSecureHandler() { 
    if (IsValid_) 
        Disconnect();
}

/********************************************************/
bool socketSecureHandler::Good() { 
    return Socket_.Good() && IsValid_; 
}

/********************************************************/
int socketSecureHandler::Connect(const TAddrList& addrs, TDuration Timeout) { 
    IsValid_ = false; 

    int ret = socketRegularHandler::Connect(addrs, Timeout);
    if (ret)
        return ret;

    gnutls_certificate_allocate_credentials(&Credits_); 
    gnutls_init(&Session_, GNUTLS_CLIENT); 
    gnutls_set_default_priority(Session_); 
    gnutls_credentials_set(Session_, GNUTLS_CRD_CERTIFICATE, Credits_); 

    SOCKET fd = Socket_; 
    gnutls_transport_set_ptr(Session_, (gnutls_transport_ptr)fd); 

    ret = gnutls_handshake(Session_); 

    if (ret < 0) { 
        fprintf(stderr, "*** Handshake failed\n");
        gnutls_perror(ret);

        gnutls_deinit(Session_); 
        if (Credits_) { 
            gnutls_certificate_free_credentials(Credits_); 
            Credits_ = 0; 
        }
        return 1;
    }

    IsValid_ = true; 
    return !IsValid_; 
}

/********************************************************/
void socketSecureHandler::Disconnect() { 
    if (IsValid_) { 
        gnutls_bye(Session_, GNUTLS_SHUT_RDWR); 
        IsValid_ = false; 
        gnutls_deinit(Session_); 
    }

    if (Credits_) { 
        gnutls_certificate_free_credentials(Credits_); 
        Credits_ = 0; 
    }

    socketRegularHandler::Disconnect();
}

/********************************************************/
void socketSecureHandler::shutdown() { 
}

/********************************************************/
bool socketSecureHandler::send(const char* message, ssize_t messlen) { 
    if (!IsValid_) 
        return false;
    ssize_t rv = gnutls_record_send(Session_, message, messlen); 
    return rv >= 0;
}

/********************************************************/
bool socketSecureHandler::peek() { 
    //ssize_t rv = gnutls_record_check_pending(mSession);
    //return rv>0;
    return true;
}

/********************************************************/
ssize_t socketSecureHandler::read(void* buffer, ssize_t buflen) { 
    if (!IsValid_) 
        return false;
    return gnutls_record_recv(Session_, (char*)buffer, buflen); 
}

#endif

/************************************************************/
socketAbstractHandler* socketHandlerFactory::chooseHandler(const THttpURL& url) { 
    if (url.IsValidGlobal() && url.GetScheme() == THttpURL::SchemeHTTP) 
        return new socketRegularHandler;

#ifdef USE_GNUTLS 
    if (url.IsValidGlobal() && url.GetScheme() == THttpURL::SchemeHTTPS) 
        return new socketSecureHandler;
#endif 

    return nullptr;
}

/************************************************************/
socketHandlerFactory socketHandlerFactory::sInstance;
/************************************************************/