Browse code

Making default log level as ERROR

Change-Id: I19a1e57c028545d6e3dce5bddcfd64984a29639a
Reviewed-on: http://photon-jenkins.eng.vmware.com:8082/3754
Tested-by: gerrit-photon <photon-checkins@vmware.com>
Reviewed-by: Kumar Kaushik <kaushikk@vmware.com>

Kumar Kaushik authored on 2017/09/13 07:48:18
Showing 3 changed files
... ...
@@ -1,7 +1,7 @@
1 1
 Name:          c-rest-engine
2 2
 Summary:       minimal http(s) server library
3 3
 Version:       1.0.4
4
-Release:       1%{?dist}
4
+Release:       2%{?dist}
5 5
 Group:         Applications/System
6 6
 Vendor:        VMware, Inc.
7 7
 License:       Apache 2.0
... ...
@@ -13,6 +13,7 @@ BuildRequires: coreutils >= 8.22
13 13
 BuildRequires: openssl-devel >= 1.0.1
14 14
 Source0:       %{name}-%{version}.tar.gz
15 15
 %define sha1   c-rest-engine=3b2e8e421f1d3d2a3932502f24c5064c993e0ad0
16
+Patch0:        logLevelError.patch
16 17
 
17 18
 %description
18 19
 c-rest-engine is a minimal embedded http(s) server written in C.
... ...
@@ -30,6 +31,7 @@ development libs and header files for c-rest-engine
30 30
 
31 31
 %prep
32 32
 %setup -q
33
+%patch0 -p1
33 34
 
34 35
 %build
35 36
 cd build
... ...
@@ -62,6 +64,8 @@ find %{buildroot} -name '*.la' -delete
62 62
 # %doc ChangeLog README COPYING
63 63
 
64 64
 %changelog
65
+*  Tue Sep 12 2017 Kumar Kaushik <kaushikk@vmware.com> 1.0.4-2
66
+-  Making default log level as ERROR.
65 67
 *  Mon Sep 11 2017 Kumar Kaushik <kaushikk@vmware.com> 1.0.4-1
66 68
 -  Updating to version 1.0.4.
67 69
 *  Tue Aug 22 2017 Kumar Kaushik <kaushikk@vmware.com> 1.0.3-2
... ...
@@ -70,7 +74,7 @@ find %{buildroot} -name '*.la' -delete
70 70
 -  Updating version to 1.0.3, API for setting SSL info.
71 71
 *  Tue Jun 20 2017 Kumar Kaushik <kaushikk@vmware.com> 1.0.2-1
72 72
 -  Updating version to 1.0.2
73
-*  Tue May 18 2017 Kumar Kaushik <kaushikk@vmware.com> 1.0.1-1
73
+*  Thu May 18 2017 Kumar Kaushik <kaushikk@vmware.com> 1.0.1-1
74 74
 -  Updating version to 1.0.1
75 75
 *  Thu May 04 2017 Priyesh Padmavilasom <ppadmavilasom@vmware.com> 1.0.0-1
76 76
 -  Initial build.  First version
77 77
new file mode 100644
... ...
@@ -0,0 +1,47 @@
0
+From 02458173e2f9eb7e931e5503f165ff59336cd3da Mon Sep 17 00:00:00 2001
1
+From: Kumar Kaushik <kaushikk@vmware.com>
2
+Date: Tue, 12 Sep 2017 14:18:25 -0700
3
+Subject: [PATCH] Making log level to ERROR
4
+
5
+Change-Id: Icbcff058e0b6fce14c2e36e72f589414e1af27b7
6
+---
7
+ build/package/rpm/c-rest-engine.spec | 6 ++++--
8
+ server/restengine/httpMain.c         | 2 +-
9
+ 2 files changed, 5 insertions(+), 3 deletions(-)
10
+
11
+diff --git a/build/package/rpm/c-rest-engine.spec b/build/package/rpm/c-rest-engine.spec
12
+index 34e85b5..3ab6aff 100644
13
+--- a/build/package/rpm/c-rest-engine.spec
14
+@@ -1,7 +1,7 @@
15
+ Name:          c-rest-engine
16
+ Summary:       Minimal http(s) server library
17
+-Version:       1.0.3
18
+-Release:       5%{?dist}
19
++Version:       1.0.4
20
++Release:       1%{?dist}
21
+ Group:         Applications/System
22
+ Vendor:        VMware, Inc.
23
+ License:       Apache 2.0
24
+@@ -65,6 +65,8 @@ find %{buildroot} -name '*.la' -delete
25
+ %{_lib64dir}/*.so
26
+ 
27
+ %changelog
28
++*   Tue Sep 12 2017 Kumar Kaushik <kaushikk@vmware.com> 1.0.4-1
29
++-   Making log level to ERROR.
30
+ *   Thu Aug 17 2017 Kumar Kaushik <kaushikk@vmware.com> 1.0.3-5
31
+ -   Adding new and cleaner parsing for all packets.
32
+ *   Mon Aug 14 2017 Kumar Kaushik <kaushikk@vmware.com> 1.0.3-4
33
+diff --git a/server/restengine/httpMain.c b/server/restengine/httpMain.c
34
+index 5c1b15a..7075e5d 100644
35
+--- a/server/restengine/httpMain.c
36
+@@ -29,7 +29,7 @@ VmHTTPInit(
37
+     }
38
+     BAIL_ON_VMREST_ERROR(dwError);
39
+ 
40
+-    pRESTHandle->debugLogLevel = VMREST_LOG_LEVEL_DEBUG;
41
++    pRESTHandle->debugLogLevel = VMREST_LOG_LEVEL_ERROR;
42
+ 
43
+     if (pConfig != NULL)
44
+     {
0 45
deleted file mode 100644
... ...
@@ -1,2819 +0,0 @@
1
-diff -ru c-rest-engine-1.0.3/build/package/rpm/c-rest-engine.spec c-rest-engine-1.0.4/build/package/rpm/c-rest-engine.spec
2
-+++ c-rest-engine-1.0.4/build/package/rpm/c-rest-engine.spec	2017-08-21 16:40:55.000000000 -0700
3
-@@ -1,7 +1,7 @@
4
- Name:          c-rest-engine
5
- Summary:       Minimal http(s) server library
6
--Version:       1.0.2
7
--Release:       2%{?dist}
8
-+Version:       1.0.3
9
-+Release:       5%{?dist}
10
- Group:         Applications/System
11
- Vendor:        VMware, Inc.
12
- License:       Apache 2.0
13
-@@ -65,11 +65,19 @@
14
- %{_lib64dir}/*.so
15
- 
16
- %changelog
17
-+*   Thu Aug 17 2017 Kumar Kaushik <kaushikk@vmware.com> 1.0.3-5
18
-+-   Adding new and cleaner parsing for all packets.
19
-+*   Mon Aug 14 2017 Kumar Kaushik <kaushikk@vmware.com> 1.0.3-4
20
-+-   Fixing bug # 1938177
21
-+*   Fri Aug 11 2017 Kumar Kaushik <kaushikk@vmware.com> 1.0.3-3
22
-+-   Fixing all known coverity bugs.
23
-+*   Fri Aug 04 2017 Kumar Kaushik <kaushikk@vmware.com> 1.0.3-2
24
-+-   Applying security fixes for set SSL info.
25
- *   Thu Jul 20 2017 Kumar Kaushik <kaushikk@vmware.com> 1.0.2-2
26
- -   Providing API for set SSL info, Bug#1864924
27
- *   Mon Jun 19 2017 Kumar Kaushik <kaushikk@vmware.com> 1.0.2-1
28
- -   Updating to version 1.0.2
29
--*   Thu Jun 16 2017 Kumar Kaushik <kaushikk@vmware.com> 1.0.1-4
30
-+*   Fri Jun 16 2017 Kumar Kaushik <kaushikk@vmware.com> 1.0.1-4
31
- -   Relaxing maximum URI length.
32
- *   Thu Jun 08 2017 Kumar Kaushik <kaushikk@vmware.com> 1.0.1-3
33
- -   Fixing file upload.
34
-diff -ru c-rest-engine-1.0.3/common/logging.c c-rest-engine-1.0.4/common/logging.c
35
-+++ c-rest-engine-1.0.4/common/logging.c	2017-08-21 16:40:55.000000000 -0700
36
-@@ -56,8 +56,8 @@
37
-     if (pRESTHandle && pRESTHandle->logFile != NULL)
38
-     {
39
-        fclose(pRESTHandle->logFile);
40
-+       pRESTHandle->logFile = NULL;
41
-     }
42
--    pRESTHandle->logFile = NULL;
43
- }
44
- 
45
- void
46
-diff -ru c-rest-engine-1.0.3/common/sockinterface.c c-rest-engine-1.0.4/common/sockinterface.c
47
-+++ c-rest-engine-1.0.4/common/sockinterface.c	2017-08-21 16:40:55.000000000 -0700
48
-@@ -1,908 +1,919 @@
49
--/* C-REST-Engine
50
--*
51
--* Copyright (c) 2017 VMware, Inc. All Rights Reserved. 
52
--*
53
--* This product is licensed to you under the Apache 2.0 license (the "License").
54
--* You may not use this product except in compliance with the Apache 2.0 License.  
55
--*
56
--* This product may include a number of subcomponents with separate copyright 
57
--* notices and license terms. Your use of these subcomponents is subject to the 
58
--* terms and conditions of the subcomponent's license, as noted in the LICENSE file. 
59
--*
60
--*/
61
--
62
--#include "includes.h"
63
--
64
--static
65
--VOID
66
--VmRESTSockContextFree(
67
--    PVMREST_HANDLE                   pRESTHandle,
68
--    PVMREST_SOCK_CONTEXT             pSockInterface
69
--    );
70
--
71
--static
72
--DWORD
73
--VmRESTHandleSocketEvent(
74
--    PVMREST_HANDLE                   pRESTHandle,
75
--    PVM_SOCKET                       pSocket,
76
--    VM_SOCK_EVENT_TYPE               sockEvent,
77
--    PVM_SOCK_IO_BUFFER               pIoBuffer,
78
--    DWORD                            dwError
79
--    );
80
--
81
--static
82
--DWORD
83
--VmRESTOnNewConnection(
84
--    PVMREST_HANDLE                   pRESTHandle,
85
--    PVM_SOCKET                       pSocket,
86
--    PVM_SOCK_IO_BUFFER               pIoBuffer
87
--    );
88
--
89
--static
90
--VOID
91
--VmRESTOnDisconnect(
92
--    PVMREST_HANDLE                   pRESTHandle,
93
--    PVM_SOCKET                       pSocket,
94
--    PVM_SOCK_IO_BUFFER               pIoBuffer
95
--    );
96
--
97
--static
98
--DWORD
99
--VmRESTOnDataAvailable(
100
--    PVMREST_HANDLE                   pRESTHandle,
101
--    PVM_SOCKET                       pSocket,
102
--    PVM_SOCK_IO_BUFFER               pIoBuffer
103
--    );
104
--
105
--static
106
--PVOID
107
--VmRESTSockWorkerThreadProc(
108
--    PVOID                            pData
109
--    );
110
--
111
--static
112
--DWORD
113
--VmRESTTcpReceiveNewData(
114
--    PVMREST_HANDLE                   pRESTHandle,
115
--    PVM_SOCKET                       pSocket
116
--    );
117
--
118
--static
119
--DWORD
120
--VmRESTReceiveData(
121
--    PVMREST_HANDLE                   pRESTHandle,
122
--    PVM_SOCKET                       pSocket,
123
--    PVM_SOCK_IO_BUFFER               pIoBuffer
124
--    );
125
--
126
--static
127
--DWORD
128
--VmRESTTcpReceiveData(
129
--    PVMREST_HANDLE                   pRESTHandle,
130
--    PVM_SOCKET                       pSocket,
131
--    PVM_SOCK_IO_BUFFER               pIoBuffer
132
--    );
133
--
134
--static
135
--DWORD
136
--VmRESTDisconnectClient(
137
--    PVMREST_HANDLE                   pRESTHandle,
138
--    PVM_SOCKET                       pSocket
139
--    );
140
--
141
--static
142
--PVOID
143
--VmRESTSockWorkerThreadProc(
144
--    PVOID                            pData
145
--    );
146
--
147
--DWORD
148
--VmRESTInitProtocolServer(
149
--    PVMREST_HANDLE                   pRESTHandle
150
--    )
151
--{
152
--    DWORD                            dwError = REST_ENGINE_SUCCESS;
153
--    PVMREST_SOCK_CONTEXT             pSockContext = NULL;
154
--    DWORD                            dwFlags = VM_SOCK_CREATE_FLAGS_REUSE_ADDR |
155
--                                               VM_SOCK_CREATE_FLAGS_NON_BLOCK;
156
--    DWORD                            iThr = 0;
157
--    char                             lastPortChar = '\0';
158
--    char*                            sslCert = NULL;
159
--    char*                            sslKey = NULL;
160
--    char*                            temp = NULL;
161
--    PVM_WORKER_THREAD_DATA           pThreadData = NULL;
162
--
163
--    if (! pRESTHandle || !( pRESTHandle->pRESTConfig))
164
--    {
165
--        VMREST_LOG_ERROR(pRESTHandle,"%s","Invalid REST config");
166
--        dwError = REST_ERROR_INVALID_HANDLER;
167
--    }
168
--    BAIL_ON_VMREST_ERROR(dwError);
169
--
170
--    pSockContext =  pRESTHandle->pSockContext;
171
--
172
--    dwError = VmRESTAllocateMutex(&pSockContext->pMutex);
173
--    BAIL_ON_VMREST_ERROR(dwError);
174
--
175
--    /**** Init SSL if configured ****/
176
--    if (( pRESTHandle->pRESTConfig->server_port != NULL) && (strlen( pRESTHandle->pRESTConfig->server_port) == 0))
177
--    {
178
--        VMREST_LOG_ERROR(pRESTHandle,"%s","REST Engine config server port missing");
179
--        dwError = 111;  /** Fix this **/
180
--    }
181
--    BAIL_ON_VMREST_ERROR(dwError);
182
--
183
--    lastPortChar = VmRESTUtilsGetLastChar(
184
--                        pRESTHandle->pRESTConfig->server_port
185
--                       );
186
--
187
--    if (lastPortChar == 'p' || lastPortChar == 'P')
188
--    {
189
--        VMREST_LOG_DEBUG(pRESTHandle,"%s","Server initing in plain text wire connection mode");
190
--        temp =  pRESTHandle->pRESTConfig->server_port;
191
--        while(temp != NULL)
192
--        {
193
--            if (*temp == 'p' || *temp == 'P')
194
--            {
195
--                *temp = '\0';
196
--                break;
197
--            }
198
--            temp++;
199
--        }
200
--    }
201
--    else
202
--    {
203
--        VMREST_LOG_DEBUG(pRESTHandle,"%s","Server initing in encrypted wire connection mode");
204
--        if (strlen( pRESTHandle->pRESTConfig->ssl_certificate) == 0 || strlen( pRESTHandle->pRESTConfig->ssl_key) == 0)
205
--        {
206
--            VMREST_LOG_ERROR(pRESTHandle,"%s", "Invalid SSL params");
207
--            dwError =  112; /** Fix this **/
208
--        }
209
--        BAIL_ON_VMREST_ERROR(dwError);
210
--        dwFlags = dwFlags | VM_SOCK_IS_SSL;
211
--        sslCert =  pRESTHandle->pRESTConfig->ssl_certificate;
212
--        sslKey =   pRESTHandle->pRESTConfig->ssl_key;
213
--    }
214
--
215
--    /**** Handle IPv4 case ****/
216
--
217
--    dwError = VmwSockOpenServer(
218
--                         pRESTHandle,
219
--                        ((unsigned short)atoi( pRESTHandle->pRESTConfig->server_port)),
220
--                        ((int)atoi( pRESTHandle->pRESTConfig->worker_thread_count)),
221
--                        dwFlags | VM_SOCK_CREATE_FLAGS_TCP |
222
--                                  VM_SOCK_CREATE_FLAGS_IPV4,
223
--                        &pSockContext->pListenerTCP,
224
--                        sslCert,
225
--                        sslKey
226
--                        );
227
--    BAIL_ON_VMREST_ERROR(dwError);
228
--
229
--#ifdef AF_INET6
230
--    /**** Handle IPv6 case ****/
231
--
232
--    dwError = VmwSockOpenServer(
233
--                   pRESTHandle,
234
--                  ((unsigned short)atoi( pRESTHandle->pRESTConfig->server_port)),
235
--                  ((int)atoi( pRESTHandle->pRESTConfig->worker_thread_count)),
236
--                  dwFlags | VM_SOCK_CREATE_FLAGS_TCP |
237
--                          VM_SOCK_CREATE_FLAGS_IPV6,
238
--                  &pSockContext->pListenerTCP6,
239
--                  sslCert,
240
--                  sslKey
241
--                  );
242
--    BAIL_ON_VMREST_ERROR(dwError);
243
--#endif
244
--
245
--    dwError = VmwSockCreateEventQueue(
246
--                   pRESTHandle,
247
--                  -1, 
248
--                  &pSockContext->pEventQueue
249
--                  );
250
--    BAIL_ON_VMREST_ERROR(dwError);
251
--
252
--    dwError = VmwSockEventQueueAdd(
253
--                   pRESTHandle,
254
--                  pSockContext->pEventQueue,
255
--                  pSockContext->pListenerTCP
256
--                  );
257
--    BAIL_ON_VMREST_ERROR(dwError);
258
--
259
--#ifdef AF_INET6
260
--    dwError = VmwSockEventQueueAdd(
261
--                   pRESTHandle,
262
--                  pSockContext->pEventQueue,
263
--                  pSockContext->pListenerTCP6
264
--                  );
265
--    BAIL_ON_VMREST_ERROR(dwError);
266
--#endif
267
--
268
--    dwError = VmRESTAllocateMemory(
269
--                  sizeof(PVMREST_THREAD) * (((int)atoi( pRESTHandle->pRESTConfig->worker_thread_count))) ,
270
--                  (PVOID*)&pSockContext->pWorkerThreads
271
--                  );
272
--    BAIL_ON_VMREST_ERROR(dwError);
273
--
274
--    pSockContext->dwNumThreads = ((int)atoi( pRESTHandle->pRESTConfig->worker_thread_count));
275
--
276
--    for (; iThr < pSockContext->dwNumThreads; iThr++)
277
--    {
278
--        dwError = VmRESTAllocateMemory(
279
--                  sizeof(VM_WORKER_THREAD_DATA) ,
280
--                  (PVOID*)&pThreadData
281
--                  );
282
--        BAIL_ON_VMREST_ERROR(dwError);
283
--        pThreadData->pSockContext = pSockContext;
284
--        pThreadData-> pRESTHandle =  pRESTHandle;
285
--
286
--        dwError = VmRESTAllocateMemory(
287
--                      sizeof(VMREST_THREAD),
288
--                      (void **)&pSockContext->pWorkerThreads[iThr]
289
--                      );
290
--        BAIL_ON_VMREST_ERROR(dwError);
291
--
292
--        dwError = VmRESTCreateThread(
293
--                      pSockContext->pWorkerThreads[iThr],
294
--                      TRUE,
295
--                      (PVMREST_START_ROUTINE)&VmRESTSockWorkerThreadProc,
296
--                      pThreadData
297
--                      );
298
--        BAIL_ON_VMREST_ERROR(dwError);
299
--
300
--        pThreadData = NULL;
301
--    }
302
--
303
--     pRESTHandle->pSockContext = pSockContext;
304
--
305
--cleanup:
306
--
307
--    return dwError;
308
--
309
--error:
310
--
311
--    goto cleanup;
312
--}
313
--
314
--VOID
315
--VmRESTShutdownProtocolServer(
316
--    PVMREST_HANDLE                   pRESTHandle
317
--    )
318
--{
319
--    if (pRESTHandle && pRESTHandle->pSockContext)
320
--    {
321
--         VmRESTSockContextFree( pRESTHandle,  pRESTHandle->pSockContext);
322
--    }
323
--}
324
--
325
--static
326
--PVOID
327
--VmRESTSockWorkerThreadProc(
328
--    PVOID                            pData
329
--    )
330
--{
331
--    DWORD                            dwError = 0;
332
--    PVM_WORKER_THREAD_DATA           pWorkerData = (PVM_WORKER_THREAD_DATA)pData;
333
--    PVMREST_HANDLE                   pRESTHandle = pWorkerData-> pRESTHandle;
334
--    PVMREST_SOCK_CONTEXT             pSockContext = pWorkerData->pSockContext;
335
--    PVM_SOCKET                       pSocket = NULL;
336
--    PVM_SOCK_IO_BUFFER               pIoBuffer = NULL;
337
--
338
--    if (pWorkerData != NULL)
339
--    {
340
--        VmRESTFreeMemory(pWorkerData);
341
--        pWorkerData = NULL;
342
--    }
343
--
344
--    for(;;)
345
--    {
346
--        VM_SOCK_EVENT_TYPE eventType = VM_SOCK_EVENT_TYPE_UNKNOWN;
347
--
348
--        dwError = VmwSockWaitForEvent(
349
--                         pRESTHandle,
350
--                        pSockContext->pEventQueue,
351
--                        -1,
352
--                        &pSocket,
353
--                        &eventType,
354
--                        &pIoBuffer);
355
--
356
--        if (dwError == ERROR_SHUTDOWN_IN_PROGRESS)
357
--        {
358
--            break;
359
--        }
360
--        dwError = VmRESTHandleSocketEvent(
361
--                         pRESTHandle,
362
--                        pSocket,
363
--                        eventType,
364
--                        pIoBuffer,
365
--                        dwError);
366
--
367
--        if (dwError == ERROR_SUCCESS ||
368
--            dwError == ERROR_IO_PENDING)
369
--        { 
370
--            dwError = ERROR_SUCCESS;
371
--        }
372
--        else
373
--        {
374
--            pSocket = NULL;
375
--            pIoBuffer = NULL;
376
--            dwError = 0;
377
--        }
378
--        BAIL_ON_VMREST_ERROR(dwError);
379
--   
380
--    }
381
--error:
382
--#ifndef WIN32
383
--    if (pSocket)
384
--    {
385
--        VmwSockRelease( pRESTHandle, pSocket);
386
--    }
387
--#endif
388
--
389
--    return NULL;
390
--}
391
--
392
--
393
--
394
--static
395
--DWORD
396
--VmRESTHandleSocketEvent(
397
--    PVMREST_HANDLE                   pRESTHandle,
398
--    PVM_SOCKET                       pSocket,
399
--    VM_SOCK_EVENT_TYPE               sockEvent,
400
--    PVM_SOCK_IO_BUFFER               pIoBuffer,
401
--    DWORD                            dwError
402
--    )
403
--{
404
--    if (dwError == ERROR_SUCCESS)
405
--    {
406
--        switch (sockEvent)
407
--        {
408
--        case VM_SOCK_EVENT_TYPE_TCP_NEW_CONNECTION:
409
--
410
--            dwError = VmRESTOnNewConnection( pRESTHandle, pSocket, pIoBuffer);
411
--            BAIL_ON_VMREST_ERROR(dwError);
412
--            break;
413
--#ifndef WIN32
414
--        case VM_SOCK_EVENT_TYPE_DATA_AVAILABLE:
415
--            dwError = VmRESTOnDataAvailable( pRESTHandle,pSocket, pIoBuffer);
416
--            BAIL_ON_VMREST_ERROR(dwError);
417
--            break;
418
--
419
--        case VM_SOCK_EVENT_TYPE_CONNECTION_CLOSED:
420
--            VmRESTOnDisconnect( pRESTHandle, pSocket, pIoBuffer);
421
--            break;
422
--
423
--        case VM_SOCK_EVENT_TYPE_UNKNOWN:
424
--             dwError = ERROR_INVALID_STATE;
425
--             break;
426
--
427
--        default:
428
--            dwError = ERROR_INVALID_MESSAGE;
429
--            break;
430
--#endif
431
--        }
432
--    }
433
--
434
--cleanup:
435
--
436
--    return dwError;
437
--
438
--error :
439
--    goto cleanup;
440
--}
441
--
442
--static
443
--DWORD
444
--VmRESTOnNewConnection(
445
--    PVMREST_HANDLE                   pRESTHandle,
446
--    PVM_SOCKET                       pSocket,
447
--    PVM_SOCK_IO_BUFFER               pIoBuffer
448
--    )
449
--{
450
--    DWORD                            dwError = REST_ENGINE_SUCCESS;
451
--    if (!pSocket)
452
--    {
453
--        dwError = ERROR_INVALID_PARAMETER;
454
--        BAIL_ON_VMREST_ERROR(dwError);
455
--    }
456
--
457
--#ifdef WIN32
458
--    dwError = VmRESTTcpReceiveNewData( pRESTHandle, pSocket);
459
--    BAIL_ON_VMREST_ERROR(dwError);
460
--#endif
461
--cleanup:
462
--
463
--    return dwError;
464
--
465
--error:
466
--
467
--    goto cleanup;
468
--}
469
--
470
--static
471
--VOID
472
--VmRESTOnDisconnect(
473
--    PVMREST_HANDLE                   pRESTHandle,
474
--    PVM_SOCKET                       pSocket,
475
--    PVM_SOCK_IO_BUFFER               pIoBuffer
476
--    )
477
--{
478
--    if (pSocket)
479
--    {
480
--        VmwSockClose( pRESTHandle, pSocket);
481
--    }
482
--
483
--    if (pIoBuffer)
484
--    {
485
--        VmwSockReleaseIoBuffer( pRESTHandle, pIoBuffer);
486
--    }
487
--}
488
--
489
--static
490
--DWORD
491
--VmRESTOnDataAvailable(
492
--    PVMREST_HANDLE                   pRESTHandle,
493
--    PVM_SOCKET                       pSocket,
494
--    PVM_SOCK_IO_BUFFER               pIoBuffer
495
--    )
496
--{
497
--    DWORD dwError = REST_ENGINE_SUCCESS;
498
--
499
--    if (!pSocket)
500
--    {
501
--        dwError = ERROR_INVALID_PARAMETER;
502
--        BAIL_ON_VMREST_ERROR(dwError);
503
--    }
504
--
505
--    dwError = VmRESTReceiveData( pRESTHandle,pSocket, pIoBuffer);
506
--    BAIL_ON_VMREST_ERROR(dwError);
507
--
508
--cleanup:
509
--    if (pIoBuffer)
510
--    {
511
--        VmwSockReleaseIoBuffer( pRESTHandle, pIoBuffer);
512
--    }
513
--
514
--    return dwError;
515
--
516
--error:
517
--
518
--    goto cleanup;
519
--}
520
--
521
--static
522
--DWORD
523
--VmRESTReceiveData(
524
--    PVMREST_HANDLE                   pRESTHandle,
525
--    PVM_SOCKET                       pSocket,
526
--    PVM_SOCK_IO_BUFFER               pIoBuffer
527
--    )
528
--{
529
--    DWORD                            dwError = REST_ENGINE_SUCCESS;
530
--
531
--    if (!pSocket)
532
--    {
533
--        dwError = ERROR_INVALID_PARAMETER;
534
--        BAIL_ON_VMREST_ERROR(dwError);
535
--    }
536
--
537
--    dwError = VmRESTTcpReceiveData( pRESTHandle,pSocket, pIoBuffer);
538
--    BAIL_ON_VMREST_ERROR(dwError);
539
--
540
--cleanup:
541
--    if (pIoBuffer)
542
--    {
543
--        VmwSockReleaseIoBuffer( pRESTHandle, pIoBuffer);
544
--    }
545
--
546
--    return dwError;
547
--
548
--error:
549
--
550
--    goto cleanup;
551
--
552
--}
553
--
554
--static
555
--DWORD
556
--VmRESTTcpReceiveData(
557
--    PVMREST_HANDLE                   pRESTHandle,
558
--    PVM_SOCKET                       pSocket,
559
--    PVM_SOCK_IO_BUFFER               pIoBuffer
560
--    )
561
--{
562
--    DWORD                            dwError = REST_ENGINE_SUCCESS;
563
--
564
--    if (!pSocket)
565
--    {
566
--        dwError = ERROR_INVALID_PARAMETER;
567
--        BAIL_ON_VMREST_ERROR(dwError);
568
--    }
569
--
570
--    if (!pIoBuffer)
571
--    {
572
--        dwError = VmRESTTcpReceiveNewData( pRESTHandle,pSocket);
573
--        BAIL_ON_VMREST_ERROR(dwError);
574
--    }
575
--
576
--cleanup:
577
--    if (pIoBuffer)
578
--    {
579
--        VmwSockReleaseIoBuffer( pRESTHandle, pIoBuffer);
580
--    }
581
--
582
--    return dwError;
583
--
584
--error:
585
--
586
--    goto cleanup;
587
--}
588
--
589
--static
590
--DWORD
591
--VmRESTTcpReceiveNewData(
592
--    PVMREST_HANDLE                   pRESTHandle,
593
--    PVM_SOCKET                       pSocket
594
--    )
595
--{
596
--    DWORD                            dwError = REST_ENGINE_SUCCESS;
597
--    char                             appBuffer[MAX_DATA_BUFFER_LEN] = {0};
598
--    uint32_t                         bytesRead = 0;
599
--
600
--    dwError = VmsockPosixGetXBytes(
601
--                   pRESTHandle,
602
--                  MAX_DATA_BUFFER_LEN,
603
--                  appBuffer,
604
--                  pSocket,
605
--                  &bytesRead,
606
--                  0
607
--                  );
608
--     BAIL_ON_VMREST_ERROR(dwError);
609
--
610
--
611
--     if (bytesRead > 0)
612
--     {
613
--         VMREST_LOG_DEBUG(pRESTHandle,"%s","Starting HTTP Parsing.");
614
--         dwError = VmRESTProcessIncomingData(
615
--                        pRESTHandle,
616
--                       appBuffer,
617
--                       bytesRead,
618
--                       pSocket
619
--                       );
620
--         BAIL_ON_VMREST_ERROR(dwError);
621
--     }
622
--
623
--
624
--cleanup:
625
--    VMREST_LOG_DEBUG(pRESTHandle,"%s","Calling closed connection....");
626
--    VmRESTDisconnectClient( pRESTHandle, pSocket);
627
--
628
--    return dwError;
629
--
630
--error:
631
--
632
--    goto cleanup;
633
--}
634
--
635
--static
636
--VOID
637
--VmRESTSockContextFree(
638
--    PVMREST_HANDLE                   pRESTHandle,
639
--    PVMREST_SOCK_CONTEXT             pSockContext
640
--    )
641
--{
642
--    if (pSockContext->pEventQueue)
643
--    {
644
--        VmwSockCloseEventQueue( pRESTHandle, pSockContext->pEventQueue);
645
--    }
646
--    if (pSockContext->pListenerTCP)
647
--    {
648
--        VmwSockRelease( pRESTHandle, pSockContext->pListenerTCP);
649
--    }
650
--    if (pSockContext->pListenerTCP6)
651
--    {
652
--        VmwSockRelease( pRESTHandle, pSockContext->pListenerTCP6);
653
--    }
654
--    if (pSockContext->pWorkerThreads)
655
--    {
656
--        DWORD iThr = 0;
657
--
658
--        for (; iThr < pSockContext->dwNumThreads; iThr++)
659
--        {
660
--            PVMREST_THREAD pThread = pSockContext->pWorkerThreads[iThr];
661
--
662
--            if (pThread)
663
--            {
664
--                VmRESTFreeThread(pThread);
665
--            }
666
--        }
667
--
668
--        VmRESTFreeMemory(pSockContext->pWorkerThreads);
669
--    }
670
--    if (pSockContext->pMutex)
671
--    {
672
--        VmRESTFreeMutex(pSockContext->pMutex);
673
--    }
674
--}
675
--
676
--static
677
--DWORD
678
--VmRESTDisconnectClient(
679
--    PVMREST_HANDLE                   pRESTHandle,
680
--    PVM_SOCKET                       pSocket
681
--    )
682
--{
683
--    DWORD                            dwError = REST_ENGINE_SUCCESS;
684
--
685
--    if (!pSocket)
686
--    {
687
--        dwError = ERROR_INVALID_PARAMETER;
688
--        BAIL_ON_VMREST_ERROR(dwError);
689
--    }
690
--
691
--    VmwSockClose( pRESTHandle, pSocket);
692
--    VmwSockRelease( pRESTHandle, pSocket);
693
--
694
--cleanup:
695
--
696
--    return dwError;
697
--
698
--error:
699
--
700
--    goto cleanup;
701
--}
702
--
703
--uint32_t
704
--VmsockPosixGetXBytes(
705
--    PVMREST_HANDLE                   pRESTHandle,
706
--    uint32_t                         bytesRequested,
707
--    char*                            appBuffer,
708
--    PVM_SOCKET                       pSocket,
709
--    uint32_t*                        bytesRead,
710
--    uint8_t                          shouldBlock
711
--    )
712
--{
713
--    uint32_t                         dwError = REST_ENGINE_SUCCESS;
714
--    uint32_t                         dataIndex = 0;
715
--    uint32_t                         remainingBytes = 0;
716
--    uint32_t                         dataAvailableInCache = 0;
717
--    PVM_SOCK_IO_BUFFER               pIoBuffer = NULL;
718
--    PVM_STREAM_BUFFER                pStreamBuffer = NULL;
719
--
720
--    if (bytesRequested > MAX_DATA_BUFFER_LEN || appBuffer == NULL || bytesRead == NULL)
721
--    {
722
--       VMREST_LOG_DEBUG(pRESTHandle,"%s","Bytes to be read %u Large or appBuffer: %s",
723
--                         bytesRequested, appBuffer);
724
--        dwError = VMREST_TRANSPORT_INVALID_PARAM;
725
--    }
726
--    BAIL_ON_VMREST_ERROR(dwError);
727
--
728
--    if (sizeof(appBuffer) > MAX_DATA_BUFFER_LEN)
729
--    {
730
--        VMREST_LOG_DEBUG(pRESTHandle,"%s","ERROR: Application buffer size too large");
731
--        dwError = VMREST_TRANSPORT_INVALID_PARAM;
732
--    }
733
--    BAIL_ON_VMREST_ERROR(dwError);
734
--
735
--    VmwSockGetStreamBuffer( pRESTHandle, pSocket, &pStreamBuffer);
736
--
737
--    if (!pStreamBuffer)
738
--    {
739
--        dwError = 500;
740
--    }
741
--    BAIL_ON_VMREST_ERROR(dwError);
742
--
743
--    dataIndex = pStreamBuffer->dataProcessed;
744
--
745
--    dataAvailableInCache = pStreamBuffer->dataRead - pStreamBuffer->dataProcessed;
746
--
747
--    if (dataAvailableInCache >= bytesRequested)
748
--    {
749
--        /**** Enough data available in stream cache buffer ****/
750
--        memcpy(appBuffer,
751
--               &(pStreamBuffer->pData[dataIndex]),
752
--               bytesRequested
753
--               );
754
--        pStreamBuffer->dataProcessed += bytesRequested;
755
--        *bytesRead = bytesRequested;
756
--    }
757
--    else if(dataAvailableInCache < bytesRequested)
758
--    {
759
--        /**** Copy all remaining client Stream bytes and perform read ****/
760
--        if (dataAvailableInCache > 0)
761
--        {
762
--            memcpy(appBuffer,
763
--                   &(pStreamBuffer->pData[dataIndex]),
764
--                   dataAvailableInCache
765
--                   );
766
--            pStreamBuffer->dataProcessed += dataAvailableInCache;
767
--            /**** This will be overwritten in case of success ****/
768
--            *bytesRead = dataAvailableInCache;
769
--        }
770
--
771
--        dwError = VmwSockAllocateIoBuffer(
772
--                         pRESTHandle,
773
--                        VM_SOCK_EVENT_TYPE_TCP_REQUEST_DATA_READ,
774
--                        MAX_DATA_BUFFER_LEN,
775
--                        &pIoBuffer
776
--                        );
777
--        BAIL_ON_VMREST_ERROR(dwError);
778
--        dwError = VmwSockRead(
779
--                             pRESTHandle,
780
--                            pSocket,
781
--                            pIoBuffer);
782
--	//VMREST_LOG_DEBUG(pRESTHandle,"SockRead(), dwError = %u, dataRead %u", dwError, pIoBuffer->dwBytesTransferred);
783
--        if (dwError == ERROR_SUCCESS)
784
--        {
785
--            memset(pStreamBuffer->pData, '\0', MAX_DATA_BUFFER_LEN);
786
--            memcpy(pStreamBuffer->pData, pIoBuffer->pData,pIoBuffer->dwBytesTransferred);
787
--			pStreamBuffer->dataProcessed = 0;
788
--			pStreamBuffer->dataRead = pIoBuffer->dwBytesTransferred;
789
--			BAIL_ON_VMREST_ERROR(dwError);
790
--        }
791
--        else if (dwError == ERROR_IO_PENDING)
792
--        {
793
--            // fail for linux?
794
--#ifndef WIN32
795
--            pIoBuffer = NULL;
796
--#endif 
797
--        }
798
--        else
799
--        {
800
--            BAIL_ON_VMREST_ERROR(dwError);
801
--        }
802
--
803
--        remainingBytes = bytesRequested - dataAvailableInCache;
804
--        dataIndex = 0;
805
--
806
--        if (remainingBytes > pStreamBuffer->dataRead)
807
--        {
808
--            remainingBytes = pStreamBuffer->dataRead;
809
--            VMREST_LOG_DEBUG(pRESTHandle,"WARNING: Requested %u bytes, available only %u bytes", bytesRequested,(dataAvailableInCache + remainingBytes));
810
--        }
811
--
812
--        memcpy((appBuffer + dataAvailableInCache),
813
--              &(pStreamBuffer->pData[dataIndex]),
814
--              remainingBytes);
815
--        pStreamBuffer->dataProcessed = remainingBytes;
816
--        *bytesRead = dataAvailableInCache + remainingBytes;
817
--
818
--        //VMREST_LOG_DEBUG(pRESTHandle,"dataAvailableInCache %u, remainingBytes %u, appBuffersize %u", dataAvailableInCache, remainingBytes, strlen(appBuffer));
819
--    }
820
--
821
--    VmwSockSetStreamBuffer( pRESTHandle, pSocket, pStreamBuffer);
822
--
823
--cleanup:
824
--    if (pIoBuffer)
825
--    {
826
--        VmwSockReleaseIoBuffer( pRESTHandle, pIoBuffer);
827
--    }
828
--
829
--    return dwError;
830
--
831
--error:
832
--
833
--    goto cleanup;
834
--
835
--}
836
--
837
--uint32_t
838
--VmSockPosixAdjustProcessedBytes(
839
--    PVMREST_HANDLE                   pRESTHandle,
840
--    PVM_SOCKET                       pSocket,
841
--    uint32_t                         dataSeen
842
--)
843
--{
844
--    uint32_t                         dwError = REST_ENGINE_SUCCESS;
845
--    PVM_STREAM_BUFFER                pStreamBuffer = NULL;
846
--
847
--    if (dataSeen > MAX_DATA_BUFFER_LEN)
848
--    {
849
--       VMREST_LOG_DEBUG(pRESTHandle,"%s","Invalid new Processed Data Index %u", dataSeen);
850
--        dwError = VMREST_TRANSPORT_INVALID_PARAM;
851
--    }
852
--    BAIL_ON_VMREST_ERROR(dwError);
853
--
854
--    VmwSockGetStreamBuffer( pRESTHandle, pSocket, &pStreamBuffer);
855
--
856
--    if (!pStreamBuffer)
857
--    {
858
--        dwError = 500;
859
--    }
860
--    BAIL_ON_VMREST_ERROR(dwError);
861
--
862
--    pStreamBuffer->dataProcessed = dataSeen;
863
--
864
--    VmwSockSetStreamBuffer( pRESTHandle, pSocket, pStreamBuffer);
865
--
866
--cleanup:
867
--    return dwError;
868
--error:
869
--    goto cleanup;
870
--}
871
--
872
--uint32_t
873
--VmSockPosixDecrementProcessedBytes(
874
--    PVMREST_HANDLE                   pRESTHandle,
875
--    PVM_SOCKET                       pSocket,
876
--    uint32_t                         offset
877
--)
878
--{
879
--    uint32_t                         dwError = REST_ENGINE_SUCCESS;
880
--    PVM_STREAM_BUFFER                pStreamBuffer = NULL;
881
--
882
--    if (offset > MAX_DATA_BUFFER_LEN)
883
--    {
884
--       VMREST_LOG_DEBUG(pRESTHandle,"%s","Invalid new Processed Data Index %u", offset);
885
--        dwError = VMREST_TRANSPORT_INVALID_PARAM;
886
--    }
887
--    BAIL_ON_VMREST_ERROR(dwError);
888
--
889
--    VmwSockGetStreamBuffer( pRESTHandle, pSocket, &pStreamBuffer);
890
--
891
--    if (!pStreamBuffer)
892
--    {
893
--        dwError = 500;
894
--    }
895
--    BAIL_ON_VMREST_ERROR(dwError);
896
--
897
--    if (pStreamBuffer->dataProcessed >= offset)
898
--    {
899
--        pStreamBuffer->dataProcessed = pStreamBuffer->dataProcessed - offset;
900
--    }
901
--    VmwSockSetStreamBuffer( pRESTHandle, pSocket, pStreamBuffer);
902
--
903
--cleanup:
904
--    return dwError;
905
--error:
906
--    goto cleanup;
907
--}
908
--
909
--
910
--uint32_t
911
--VmsockPosixWriteDataAtOnce(
912
--    PVMREST_HANDLE                   pRESTHandle,
913
--    PVM_SOCKET                       pSocket,
914
--    char*                            buffer,
915
--    uint32_t                         bytes
916
--    )
917
--{
918
--    uint32_t                         dwError = REST_ENGINE_SUCCESS;
919
--    PVM_SOCK_IO_BUFFER               pIoNewBuffer = NULL;
920
--    
921
--    dwError = VmwSockAllocateIoBuffer(
922
--                   pRESTHandle,
923
--                  VM_SOCK_EVENT_TYPE_TCP_RESPONSE_DATA_WRITE,
924
--                  bytes,
925
--                  &pIoNewBuffer);
926
--    BAIL_ON_VMREST_ERROR(dwError);
927
--
928
--    memcpy(pIoNewBuffer->pData, buffer, bytes);
929
--
930
--    dwError = VmwSockWrite(
931
--                   pRESTHandle,
932
--                  pSocket,
933
--                  NULL,
934
--                  0,
935
--                  pIoNewBuffer
936
--                  );
937
--    if (dwError == ERROR_SUCCESS)
938
--    {
939
--        dwError = REST_ENGINE_SUCCESS;
940
--        BAIL_ON_VMREST_ERROR(dwError);
941
--    }
942
--    else if (dwError == ERROR_IO_PENDING)
943
--    {
944
--        pIoNewBuffer = NULL;
945
--        BAIL_ON_VMREST_ERROR(dwError);
946
--    }
947
--
948
--cleanup:
949
--    if (pIoNewBuffer)
950
--    {
951
--        VmwSockReleaseIoBuffer( pRESTHandle, pIoNewBuffer);
952
--    }
953
--    return dwError;
954
--error:
955
--    goto cleanup;
956
--}
957
-+/* C-REST-Engine
958
-+*
959
-+* Copyright (c) 2017 VMware, Inc. All Rights Reserved. 
960
-+*
961
-+* This product is licensed to you under the Apache 2.0 license (the "License").
962
-+* You may not use this product except in compliance with the Apache 2.0 License.  
963
-+*
964
-+* This product may include a number of subcomponents with separate copyright 
965
-+* notices and license terms. Your use of these subcomponents is subject to the 
966
-+* terms and conditions of the subcomponent's license, as noted in the LICENSE file. 
967
-+*
968
-+*/
969
-+
970
-+#include "includes.h"
971
-+
972
-+static
973
-+VOID
974
-+VmRESTSockContextFree(
975
-+    PVMREST_HANDLE                   pRESTHandle,
976
-+    PVMREST_SOCK_CONTEXT             pSockInterface
977
-+    );
978
-+
979
-+static
980
-+DWORD
981
-+VmRESTHandleSocketEvent(
982
-+    PVMREST_HANDLE                   pRESTHandle,
983
-+    PVM_SOCKET                       pSocket,
984
-+    VM_SOCK_EVENT_TYPE               sockEvent,
985
-+    PVM_SOCK_IO_BUFFER               pIoBuffer,
986
-+    DWORD                            dwError
987
-+    );
988
-+
989
-+static
990
-+DWORD
991
-+VmRESTOnNewConnection(
992
-+    PVMREST_HANDLE                   pRESTHandle,
993
-+    PVM_SOCKET                       pSocket,
994
-+    PVM_SOCK_IO_BUFFER               pIoBuffer
995
-+    );
996
-+
997
-+static
998
-+VOID
999
-+VmRESTOnDisconnect(
1000
-+    PVMREST_HANDLE                   pRESTHandle,
1001
-+    PVM_SOCKET                       pSocket,
1002
-+    PVM_SOCK_IO_BUFFER               pIoBuffer
1003
-+    );
1004
-+
1005
-+static
1006
-+DWORD
1007
-+VmRESTOnDataAvailable(
1008
-+    PVMREST_HANDLE                   pRESTHandle,
1009
-+    PVM_SOCKET                       pSocket,
1010
-+    PVM_SOCK_IO_BUFFER               pIoBuffer
1011
-+    );
1012
-+
1013
-+static
1014
-+PVOID
1015
-+VmRESTSockWorkerThreadProc(
1016
-+    PVOID                            pData
1017
-+    );
1018
-+
1019
-+static
1020
-+DWORD
1021
-+VmRESTTcpReceiveNewData(
1022
-+    PVMREST_HANDLE                   pRESTHandle,
1023
-+    PVM_SOCKET                       pSocket
1024
-+    );
1025
-+
1026
-+static
1027
-+DWORD
1028
-+VmRESTReceiveData(
1029
-+    PVMREST_HANDLE                   pRESTHandle,
1030
-+    PVM_SOCKET                       pSocket,
1031
-+    PVM_SOCK_IO_BUFFER               pIoBuffer
1032
-+    );
1033
-+
1034
-+static
1035
-+DWORD
1036
-+VmRESTTcpReceiveData(
1037
-+    PVMREST_HANDLE                   pRESTHandle,
1038
-+    PVM_SOCKET                       pSocket,
1039
-+    PVM_SOCK_IO_BUFFER               pIoBuffer
1040
-+    );
1041
-+
1042
-+static
1043
-+DWORD
1044
-+VmRESTDisconnectClient(
1045
-+    PVMREST_HANDLE                   pRESTHandle,
1046
-+    PVM_SOCKET                       pSocket
1047
-+    );
1048
-+
1049
-+static
1050
-+PVOID
1051
-+VmRESTSockWorkerThreadProc(
1052
-+    PVOID                            pData
1053
-+    );
1054
-+
1055
-+DWORD
1056
-+VmRESTInitProtocolServer(
1057
-+    PVMREST_HANDLE                   pRESTHandle
1058
-+    )
1059
-+{
1060
-+    DWORD                            dwError = REST_ENGINE_SUCCESS;
1061
-+    PVMREST_SOCK_CONTEXT             pSockContext = NULL;
1062
-+    DWORD                            dwFlags = VM_SOCK_CREATE_FLAGS_REUSE_ADDR |
1063
-+                                               VM_SOCK_CREATE_FLAGS_NON_BLOCK;
1064
-+    DWORD                            iThr = 0;
1065
-+    char                             lastPortChar = '\0';
1066
-+    char*                            sslCert = NULL;
1067
-+    char*                            sslKey = NULL;
1068
-+    char*                            temp = NULL;
1069
-+    PVM_WORKER_THREAD_DATA           pThreadData = NULL;
1070
-+
1071
-+    if (! pRESTHandle || !( pRESTHandle->pRESTConfig))
1072
-+    {
1073
-+        VMREST_LOG_ERROR(pRESTHandle,"%s","Invalid REST config");
1074
-+        dwError = REST_ERROR_INVALID_HANDLER;
1075
-+    }
1076
-+    BAIL_ON_VMREST_ERROR(dwError);
1077
-+
1078
-+    pSockContext =  pRESTHandle->pSockContext;
1079
-+
1080
-+    dwError = VmRESTAllocateMutex(&pSockContext->pMutex);
1081
-+    BAIL_ON_VMREST_ERROR(dwError);
1082
-+
1083
-+    /**** Init SSL if configured ****/
1084
-+    if (strlen( pRESTHandle->pRESTConfig->server_port) == 0)
1085
-+    {
1086
-+        VMREST_LOG_ERROR(pRESTHandle,"%s","REST Engine config server port missing");
1087
-+        dwError = REST_ERROR_INVALID_CONFIG_PORT;
1088
-+    }
1089
-+    BAIL_ON_VMREST_ERROR(dwError);
1090
-+
1091
-+    lastPortChar = VmRESTUtilsGetLastChar(
1092
-+                        pRESTHandle->pRESTConfig->server_port
1093
-+                       );
1094
-+
1095
-+    if (lastPortChar == 'p' || lastPortChar == 'P')
1096
-+    {
1097
-+        VMREST_LOG_DEBUG(pRESTHandle,"%s","Server initing in plain text wire connection mode");
1098
-+        temp =  pRESTHandle->pRESTConfig->server_port;
1099
-+        while(temp != NULL)
1100
-+        {
1101
-+            if (*temp == 'p' || *temp == 'P')
1102
-+            {
1103
-+                *temp = '\0';
1104
-+                break;
1105
-+            }
1106
-+            temp++;
1107
-+        }
1108
-+    }
1109
-+    else
1110
-+    {
1111
-+        VMREST_LOG_DEBUG(pRESTHandle,"%s","Server initing in encrypted wire connection mode");
1112
-+        if (strlen( pRESTHandle->pRESTConfig->ssl_certificate) == 0 || strlen( pRESTHandle->pRESTConfig->ssl_key) == 0)
1113
-+        {
1114
-+            VMREST_LOG_ERROR(pRESTHandle,"%s", "Invalid SSL params");
1115
-+            dwError =  REST_ERROR_INVALID_CONFIG;
1116
-+        }
1117
-+        BAIL_ON_VMREST_ERROR(dwError);
1118
-+        dwFlags = dwFlags | VM_SOCK_IS_SSL;
1119
-+        sslCert =  pRESTHandle->pRESTConfig->ssl_certificate;
1120
-+        sslKey =   pRESTHandle->pRESTConfig->ssl_key;
1121
-+    }
1122
-+
1123
-+    /**** Handle IPv4 case ****/
1124
-+
1125
-+    dwError = VmwSockOpenServer(
1126
-+                         pRESTHandle,
1127
-+                        ((unsigned short)atoi( pRESTHandle->pRESTConfig->server_port)),
1128
-+                        ((int)atoi( pRESTHandle->pRESTConfig->worker_thread_count)),
1129
-+                        dwFlags | VM_SOCK_CREATE_FLAGS_TCP |
1130
-+                                  VM_SOCK_CREATE_FLAGS_IPV4,
1131
-+                        &pSockContext->pListenerTCP,
1132
-+                        sslCert,
1133
-+                        sslKey
1134
-+                        );
1135
-+    BAIL_ON_VMREST_ERROR(dwError);
1136
-+
1137
-+#ifdef AF_INET6
1138
-+    /**** Handle IPv6 case ****/
1139
-+
1140
-+    dwError = VmwSockOpenServer(
1141
-+                   pRESTHandle,
1142
-+                  ((unsigned short)atoi( pRESTHandle->pRESTConfig->server_port)),
1143
-+                  ((int)atoi( pRESTHandle->pRESTConfig->worker_thread_count)),
1144
-+                  dwFlags | VM_SOCK_CREATE_FLAGS_TCP |
1145
-+                          VM_SOCK_CREATE_FLAGS_IPV6,
1146
-+                  &pSockContext->pListenerTCP6,
1147
-+                  sslCert,
1148
-+                  sslKey
1149
-+                  );
1150
-+    BAIL_ON_VMREST_ERROR(dwError);
1151
-+#endif
1152
-+
1153
-+    dwError = VmwSockCreateEventQueue(
1154
-+                   pRESTHandle,
1155
-+                  -1,
1156
-+                  &pSockContext->pEventQueue
1157
-+                  );
1158
-+    BAIL_ON_VMREST_ERROR(dwError);
1159
-+
1160
-+    dwError = VmwSockEventQueueAdd(
1161
-+                   pRESTHandle,
1162
-+                  pSockContext->pEventQueue,
1163
-+                  pSockContext->pListenerTCP
1164
-+                  );
1165
-+    BAIL_ON_VMREST_ERROR(dwError);
1166
-+
1167
-+#ifdef AF_INET6
1168
-+    dwError = VmwSockEventQueueAdd(
1169
-+                   pRESTHandle,
1170
-+                  pSockContext->pEventQueue,
1171
-+                  pSockContext->pListenerTCP6
1172
-+                  );
1173
-+    BAIL_ON_VMREST_ERROR(dwError);
1174
-+#endif
1175
-+
1176
-+    dwError = VmRESTAllocateMemory(
1177
-+                  sizeof(PVMREST_THREAD) * (((int)atoi( pRESTHandle->pRESTConfig->worker_thread_count))) ,
1178
-+                  (PVOID*)&pSockContext->pWorkerThreads
1179
-+                  );
1180
-+    BAIL_ON_VMREST_ERROR(dwError);
1181
-+
1182
-+    pSockContext->dwNumThreads = ((int)atoi( pRESTHandle->pRESTConfig->worker_thread_count));
1183
-+
1184
-+    for (; iThr < pSockContext->dwNumThreads; iThr++)
1185
-+    {
1186
-+        dwError = VmRESTAllocateMemory(
1187
-+                  sizeof(VM_WORKER_THREAD_DATA) ,
1188
-+                  (PVOID*)&pThreadData
1189
-+                  );
1190
-+        BAIL_ON_VMREST_ERROR(dwError);
1191
-+        pThreadData->pSockContext = pSockContext;
1192
-+        pThreadData-> pRESTHandle =  pRESTHandle;
1193
-+
1194
-+        dwError = VmRESTAllocateMemory(
1195
-+                      sizeof(VMREST_THREAD),
1196
-+                      (void **)&pSockContext->pWorkerThreads[iThr]
1197
-+                      );
1198
-+        BAIL_ON_VMREST_ERROR(dwError);
1199
-+
1200
-+        dwError = VmRESTCreateThread(
1201
-+                      pSockContext->pWorkerThreads[iThr],
1202
-+                      TRUE,
1203
-+                      (PVMREST_START_ROUTINE)&VmRESTSockWorkerThreadProc,
1204
-+                      pThreadData
1205
-+                      );
1206
-+        BAIL_ON_VMREST_ERROR(dwError);
1207
-+
1208
-+        pThreadData = NULL;
1209
-+    }
1210
-+
1211
-+     pRESTHandle->pSockContext = pSockContext;
1212
-+
1213
-+cleanup:
1214
-+
1215
-+    return dwError;
1216
-+
1217
-+error:
1218
-+    if (pThreadData != NULL)
1219
-+    {
1220
-+        VmRESTFreeMemory(pThreadData);
1221
-+        pThreadData = NULL;
1222
-+    }
1223
-+
1224
-+    goto cleanup;
1225
-+}
1226
-+
1227
-+VOID
1228
-+VmRESTShutdownProtocolServer(
1229
-+    PVMREST_HANDLE                   pRESTHandle
1230
-+    )
1231
-+{
1232
-+    if (pRESTHandle && pRESTHandle->pSockContext)
1233
-+    {
1234
-+         VmRESTSockContextFree( pRESTHandle,  pRESTHandle->pSockContext);
1235
-+    }
1236
-+}
1237
-+
1238
-+static
1239
-+PVOID
1240
-+VmRESTSockWorkerThreadProc(
1241
-+    PVOID                            pData
1242
-+    )
1243
-+{
1244
-+    DWORD                            dwError = 0;
1245
-+    PVM_WORKER_THREAD_DATA           pWorkerData = (PVM_WORKER_THREAD_DATA)pData;
1246
-+    PVMREST_HANDLE                   pRESTHandle = NULL;
1247
-+    PVMREST_SOCK_CONTEXT             pSockContext = NULL;
1248
-+    PVM_SOCKET                       pSocket = NULL;
1249
-+    PVM_SOCK_IO_BUFFER               pIoBuffer = NULL;
1250
-+
1251
-+    if (pWorkerData != NULL)
1252
-+    {
1253
-+        pRESTHandle = pWorkerData-> pRESTHandle;
1254
-+        pSockContext = pWorkerData->pSockContext;
1255
-+        VmRESTFreeMemory(pWorkerData);
1256
-+        pWorkerData = NULL;
1257
-+    }
1258
-+    else
1259
-+    {
1260
-+        return NULL;
1261
-+    }
1262
-+
1263
-+    for(;;)
1264
-+    {
1265
-+        VM_SOCK_EVENT_TYPE eventType = VM_SOCK_EVENT_TYPE_UNKNOWN;
1266
-+
1267
-+        dwError = VmwSockWaitForEvent(
1268
-+                        pRESTHandle,
1269
-+                        pSockContext->pEventQueue,
1270
-+                        -1,
1271
-+                        &pSocket,
1272
-+                        &eventType,
1273
-+                        &pIoBuffer);
1274
-+
1275
-+        if (dwError == ERROR_SHUTDOWN_IN_PROGRESS)
1276
-+        {
1277
-+            break;
1278
-+        }
1279
-+        dwError = VmRESTHandleSocketEvent(
1280
-+                         pRESTHandle,
1281
-+                        pSocket,
1282
-+                        eventType,
1283
-+                        pIoBuffer,
1284
-+                        dwError);
1285
-+
1286
-+        if (dwError == ERROR_SUCCESS ||
1287
-+            dwError == ERROR_IO_PENDING)
1288
-+        { 
1289
-+            dwError = ERROR_SUCCESS;
1290
-+        }
1291
-+        else
1292
-+        {
1293
-+            pSocket = NULL;
1294
-+            pIoBuffer = NULL;
1295
-+            dwError = 0;
1296
-+        }
1297
-+        BAIL_ON_VMREST_ERROR(dwError);
1298
-+   
1299
-+    }
1300
-+error:
1301
-+#ifndef WIN32
1302
-+    if (pSocket)
1303
-+    {
1304
-+        VmwSockRelease( pRESTHandle, pSocket);
1305
-+    }
1306
-+#endif
1307
-+
1308
-+    return NULL;
1309
-+}
1310
-+
1311
-+
1312
-+
1313
-+static
1314
-+DWORD
1315
-+VmRESTHandleSocketEvent(
1316
-+    PVMREST_HANDLE                   pRESTHandle,
1317
-+    PVM_SOCKET                       pSocket,
1318
-+    VM_SOCK_EVENT_TYPE               sockEvent,
1319
-+    PVM_SOCK_IO_BUFFER               pIoBuffer,
1320
-+    DWORD                            dwError
1321
-+    )
1322
-+{
1323
-+    if (dwError == ERROR_SUCCESS)
1324
-+    {
1325
-+        switch (sockEvent)
1326
-+        {
1327
-+        case VM_SOCK_EVENT_TYPE_TCP_NEW_CONNECTION:
1328
-+
1329
-+            dwError = VmRESTOnNewConnection( pRESTHandle, pSocket, pIoBuffer);
1330
-+            BAIL_ON_VMREST_ERROR(dwError);
1331
-+            break;
1332
-+#ifndef WIN32
1333
-+        case VM_SOCK_EVENT_TYPE_DATA_AVAILABLE:
1334
-+            dwError = VmRESTOnDataAvailable( pRESTHandle,pSocket, pIoBuffer);
1335
-+            BAIL_ON_VMREST_ERROR(dwError);
1336
-+            break;
1337
-+
1338
-+        case VM_SOCK_EVENT_TYPE_CONNECTION_CLOSED:
1339
-+            VmRESTOnDisconnect( pRESTHandle, pSocket, pIoBuffer);
1340
-+            break;
1341
-+
1342
-+        case VM_SOCK_EVENT_TYPE_UNKNOWN:
1343
-+             dwError = ERROR_INVALID_STATE;
1344
-+             break;
1345
-+
1346
-+        default:
1347
-+            dwError = ERROR_INVALID_MESSAGE;
1348
-+            break;
1349
-+#endif
1350
-+        }
1351
-+    }
1352
-+
1353
-+cleanup:
1354
-+
1355
-+    return dwError;
1356
-+
1357
-+error :
1358
-+    goto cleanup;
1359
-+}
1360
-+
1361
-+static
1362
-+DWORD
1363
-+VmRESTOnNewConnection(
1364
-+    PVMREST_HANDLE                   pRESTHandle,
1365
-+    PVM_SOCKET                       pSocket,
1366
-+    PVM_SOCK_IO_BUFFER               pIoBuffer
1367
-+    )
1368
-+{
1369
-+    DWORD                            dwError = REST_ENGINE_SUCCESS;
1370
-+    if (!pSocket)
1371
-+    {
1372
-+        dwError = ERROR_INVALID_PARAMETER;
1373
-+        BAIL_ON_VMREST_ERROR(dwError);
1374
-+    }
1375
-+
1376
-+#ifdef WIN32
1377
-+    dwError = VmRESTTcpReceiveNewData( pRESTHandle, pSocket);
1378
-+    BAIL_ON_VMREST_ERROR(dwError);
1379
-+#endif
1380
-+cleanup:
1381
-+
1382
-+    return dwError;
1383
-+
1384
-+error:
1385
-+
1386
-+    goto cleanup;
1387
-+}
1388
-+
1389
-+static
1390
-+VOID
1391
-+VmRESTOnDisconnect(
1392
-+    PVMREST_HANDLE                   pRESTHandle,
1393
-+    PVM_SOCKET                       pSocket,
1394
-+    PVM_SOCK_IO_BUFFER               pIoBuffer
1395
-+    )
1396
-+{
1397
-+    if (pSocket)
1398
-+    {
1399
-+        VmwSockClose( pRESTHandle, pSocket);
1400
-+    }
1401
-+
1402
-+    if (pIoBuffer)
1403
-+    {
1404
-+        VmwSockReleaseIoBuffer( pRESTHandle, pIoBuffer);
1405
-+    }
1406
-+}
1407
-+
1408
-+static
1409
-+DWORD
1410
-+VmRESTOnDataAvailable(
1411
-+    PVMREST_HANDLE                   pRESTHandle,
1412
-+    PVM_SOCKET                       pSocket,
1413
-+    PVM_SOCK_IO_BUFFER               pIoBuffer
1414
-+    )
1415
-+{
1416
-+    DWORD dwError = REST_ENGINE_SUCCESS;
1417
-+
1418
-+    if (!pSocket)
1419
-+    {
1420
-+        dwError = ERROR_INVALID_PARAMETER;
1421
-+        BAIL_ON_VMREST_ERROR(dwError);
1422
-+    }
1423
-+
1424
-+    dwError = VmRESTReceiveData( pRESTHandle,pSocket, pIoBuffer);
1425
-+    BAIL_ON_VMREST_ERROR(dwError);
1426
-+
1427
-+cleanup:
1428
-+    if (pIoBuffer)
1429
-+    {
1430
-+        VmwSockReleaseIoBuffer( pRESTHandle, pIoBuffer);
1431
-+    }
1432
-+
1433
-+    return dwError;
1434
-+
1435
-+error:
1436
-+
1437
-+    goto cleanup;
1438
-+}
1439
-+
1440
-+static
1441
-+DWORD
1442
-+VmRESTReceiveData(
1443
-+    PVMREST_HANDLE                   pRESTHandle,
1444
-+    PVM_SOCKET                       pSocket,
1445
-+    PVM_SOCK_IO_BUFFER               pIoBuffer
1446
-+    )
1447
-+{
1448
-+    DWORD                            dwError = REST_ENGINE_SUCCESS;
1449
-+
1450
-+    if (!pSocket)
1451
-+    {
1452
-+        dwError = ERROR_INVALID_PARAMETER;
1453
-+        BAIL_ON_VMREST_ERROR(dwError);
1454
-+    }
1455
-+
1456
-+    dwError = VmRESTTcpReceiveData( pRESTHandle,pSocket, pIoBuffer);
1457
-+    BAIL_ON_VMREST_ERROR(dwError);
1458
-+
1459
-+cleanup:
1460
-+    if (pIoBuffer)
1461
-+    {
1462
-+        VmwSockReleaseIoBuffer( pRESTHandle, pIoBuffer);
1463
-+    }
1464
-+
1465
-+    return dwError;
1466
-+
1467
-+error:
1468
-+
1469
-+    goto cleanup;
1470
-+
1471
-+}
1472
-+
1473
-+static
1474
-+DWORD
1475
-+VmRESTTcpReceiveData(
1476
-+    PVMREST_HANDLE                   pRESTHandle,
1477
-+    PVM_SOCKET                       pSocket,
1478
-+    PVM_SOCK_IO_BUFFER               pIoBuffer
1479
-+    )
1480
-+{
1481
-+    DWORD                            dwError = REST_ENGINE_SUCCESS;
1482
-+
1483
-+    if (!pSocket)
1484
-+    {
1485
-+        dwError = ERROR_INVALID_PARAMETER;
1486
-+        BAIL_ON_VMREST_ERROR(dwError);
1487
-+    }
1488
-+
1489
-+    if (!pIoBuffer)
1490
-+    {
1491
-+        dwError = VmRESTTcpReceiveNewData( pRESTHandle,pSocket);
1492
-+        BAIL_ON_VMREST_ERROR(dwError);
1493
-+    }
1494
-+
1495
-+cleanup:
1496
-+    if (pIoBuffer)
1497
-+    {
1498
-+        VmwSockReleaseIoBuffer( pRESTHandle, pIoBuffer);
1499
-+    }
1500
-+
1501
-+    return dwError;
1502
-+
1503
-+error:
1504
-+
1505
-+    goto cleanup;
1506
-+}
1507
-+
1508
-+static
1509
-+DWORD
1510
-+VmRESTTcpReceiveNewData(
1511
-+    PVMREST_HANDLE                   pRESTHandle,
1512
-+    PVM_SOCKET                       pSocket
1513
-+    )
1514
-+{
1515
-+    DWORD                            dwError = REST_ENGINE_SUCCESS;
1516
-+    char                             appBuffer[MAX_DATA_BUFFER_LEN] = {0};
1517
-+    uint32_t                         bytesRead = 0;
1518
-+
1519
-+    dwError = VmsockPosixGetXBytes(
1520
-+                   pRESTHandle,
1521
-+                  MAX_DATA_BUFFER_LEN,
1522
-+                  appBuffer,
1523
-+                  pSocket,
1524
-+                  &bytesRead,
1525
-+                  0
1526
-+                  );
1527
-+     BAIL_ON_VMREST_ERROR(dwError);
1528
-+
1529
-+
1530
-+     if (bytesRead > 0)
1531
-+     {
1532
-+         VMREST_LOG_DEBUG(pRESTHandle,"%s","Starting HTTP Parsing.");
1533
-+         dwError = VmRESTProcessIncomingData(
1534
-+                        pRESTHandle,
1535
-+                       appBuffer,
1536
-+                       bytesRead,
1537
-+                       pSocket
1538
-+                       );
1539
-+         BAIL_ON_VMREST_ERROR(dwError);
1540
-+     }
1541
-+
1542
-+
1543
-+cleanup:
1544
-+    VMREST_LOG_DEBUG(pRESTHandle,"%s","Calling closed connection....");
1545
-+    VmRESTDisconnectClient( pRESTHandle, pSocket);
1546
-+
1547
-+    return dwError;
1548
-+
1549
-+error:
1550
-+
1551
-+    goto cleanup;
1552
-+}
1553
-+
1554
-+static
1555
-+VOID
1556
-+VmRESTSockContextFree(
1557
-+    PVMREST_HANDLE                   pRESTHandle,
1558
-+    PVMREST_SOCK_CONTEXT             pSockContext
1559
-+    )
1560
-+{
1561
-+    if (pSockContext->pEventQueue)
1562
-+    {
1563
-+        VmwSockCloseEventQueue( pRESTHandle, pSockContext->pEventQueue);
1564
-+    }
1565
-+    if (pSockContext->pListenerTCP)
1566
-+    {
1567
-+        VmwSockRelease( pRESTHandle, pSockContext->pListenerTCP);
1568
-+    }
1569
-+    if (pSockContext->pListenerTCP6)
1570
-+    {
1571
-+        VmwSockRelease( pRESTHandle, pSockContext->pListenerTCP6);
1572
-+    }
1573
-+    if (pSockContext->pWorkerThreads)
1574
-+    {
1575
-+        DWORD iThr = 0;
1576
-+
1577
-+        for (; iThr < pSockContext->dwNumThreads; iThr++)
1578
-+        {
1579
-+            PVMREST_THREAD pThread = pSockContext->pWorkerThreads[iThr];
1580
-+
1581
-+            if (pThread)
1582
-+            {
1583
-+                VmRESTFreeThread(pThread);
1584
-+            }
1585
-+        }
1586
-+
1587
-+        VmRESTFreeMemory(pSockContext->pWorkerThreads);
1588
-+    }
1589
-+    if (pSockContext->pMutex)
1590
-+    {
1591
-+        VmRESTFreeMutex(pSockContext->pMutex);
1592
-+    }
1593
-+}
1594
-+
1595
-+static
1596
-+DWORD
1597
-+VmRESTDisconnectClient(
1598
-+    PVMREST_HANDLE                   pRESTHandle,
1599
-+    PVM_SOCKET                       pSocket
1600
-+    )
1601
-+{
1602
-+    DWORD                            dwError = REST_ENGINE_SUCCESS;
1603
-+
1604
-+    if (!pSocket)
1605
-+    {
1606
-+        dwError = ERROR_INVALID_PARAMETER;
1607
-+        BAIL_ON_VMREST_ERROR(dwError);
1608
-+    }
1609
-+
1610
-+    VmwSockClose( pRESTHandle, pSocket);
1611
-+    VmwSockRelease( pRESTHandle, pSocket);
1612
-+
1613
-+cleanup:
1614
-+
1615
-+    return dwError;
1616
-+
1617
-+error:
1618
-+
1619
-+    goto cleanup;
1620
-+}
1621
-+
1622
-+uint32_t
1623
-+VmsockPosixGetXBytes(
1624
-+    PVMREST_HANDLE                   pRESTHandle,
1625
-+    uint32_t                         bytesRequested,
1626
-+    char*                            appBuffer,
1627
-+    PVM_SOCKET                       pSocket,
1628
-+    uint32_t*                        bytesRead,
1629
-+    uint8_t                          shouldBlock
1630
-+    )
1631
-+{
1632
-+    uint32_t                         dwError = REST_ENGINE_SUCCESS;
1633
-+    uint32_t                         dataIndex = 0;
1634
-+    uint32_t                         remainingBytes = 0;
1635
-+    uint32_t                         dataAvailableInCache = 0;
1636
-+    PVM_SOCK_IO_BUFFER               pIoBuffer = NULL;
1637
-+    PVM_STREAM_BUFFER                pStreamBuffer = NULL;
1638
-+
1639
-+    if (bytesRequested > MAX_DATA_BUFFER_LEN || appBuffer == NULL || bytesRead == NULL)
1640
-+    {
1641
-+       VMREST_LOG_DEBUG(pRESTHandle,"%s","Bytes to be read %u Large or appBuffer: %s",
1642
-+                         bytesRequested, appBuffer);
1643
-+        dwError = VMREST_TRANSPORT_INVALID_PARAM;
1644
-+    }
1645
-+    BAIL_ON_VMREST_ERROR(dwError);
1646
-+
1647
-+    if (sizeof(appBuffer) > MAX_DATA_BUFFER_LEN)
1648
-+    {
1649
-+        VMREST_LOG_DEBUG(pRESTHandle,"%s","ERROR: Application buffer size too large");
1650
-+        dwError = VMREST_TRANSPORT_INVALID_PARAM;
1651
-+    }
1652
-+    BAIL_ON_VMREST_ERROR(dwError);
1653
-+
1654
-+    VmwSockGetStreamBuffer( pRESTHandle, pSocket, &pStreamBuffer);
1655
-+
1656
-+    if (!pStreamBuffer)
1657
-+    {
1658
-+        dwError = 500;
1659
-+    }
1660
-+    BAIL_ON_VMREST_ERROR(dwError);
1661
-+
1662
-+    dataIndex = pStreamBuffer->dataProcessed;
1663
-+
1664
-+    dataAvailableInCache = pStreamBuffer->dataRead - pStreamBuffer->dataProcessed;
1665
-+
1666
-+    if (dataAvailableInCache >= bytesRequested)
1667
-+    {
1668
-+        /**** Enough data available in stream cache buffer ****/
1669
-+        memcpy(appBuffer,
1670
-+               &(pStreamBuffer->pData[dataIndex]),
1671
-+               bytesRequested
1672
-+               );
1673
-+        pStreamBuffer->dataProcessed += bytesRequested;
1674
-+        *bytesRead = bytesRequested;
1675
-+    }
1676
-+    else if(dataAvailableInCache < bytesRequested)
1677
-+    {
1678
-+        /**** Copy all remaining client Stream bytes and perform read ****/
1679
-+        if (dataAvailableInCache > 0)
1680
-+        {
1681
-+            memcpy(appBuffer,
1682
-+                   &(pStreamBuffer->pData[dataIndex]),
1683
-+                   dataAvailableInCache
1684
-+                   );
1685
-+            pStreamBuffer->dataProcessed += dataAvailableInCache;
1686
-+            /**** This will be overwritten in case of success ****/
1687
-+            *bytesRead = dataAvailableInCache;
1688
-+        }
1689
-+
1690
-+        dwError = VmwSockAllocateIoBuffer(
1691
-+                         pRESTHandle,
1692
-+                        VM_SOCK_EVENT_TYPE_TCP_REQUEST_DATA_READ,
1693
-+                        MAX_DATA_BUFFER_LEN,
1694
-+                        &pIoBuffer
1695
-+                        );
1696
-+        BAIL_ON_VMREST_ERROR(dwError);
1697
-+        dwError = VmwSockRead(
1698
-+                             pRESTHandle,
1699
-+                            pSocket,
1700
-+                            pIoBuffer);
1701
-+	//VMREST_LOG_DEBUG(pRESTHandle,"SockRead(), dwError = %u, dataRead %u", dwError, pIoBuffer->dwBytesTransferred);
1702
-+        if (dwError == ERROR_SUCCESS)
1703
-+        {
1704
-+            memset(pStreamBuffer->pData, '\0', MAX_DATA_BUFFER_LEN);
1705
-+            memcpy(pStreamBuffer->pData, pIoBuffer->pData,pIoBuffer->dwBytesTransferred);
1706
-+			pStreamBuffer->dataProcessed = 0;
1707
-+			pStreamBuffer->dataRead = pIoBuffer->dwBytesTransferred;
1708
-+			BAIL_ON_VMREST_ERROR(dwError);
1709
-+        }
1710
-+        else if (dwError == ERROR_IO_PENDING)
1711
-+        {
1712
-+            // fail for linux?
1713
-+#ifndef WIN32
1714
-+            pIoBuffer = NULL;
1715
-+#endif 
1716
-+        }
1717
-+        else
1718
-+        {
1719
-+            BAIL_ON_VMREST_ERROR(dwError);
1720
-+        }
1721
-+
1722
-+        remainingBytes = bytesRequested - dataAvailableInCache;
1723
-+        dataIndex = 0;
1724
-+
1725
-+        if (remainingBytes > pStreamBuffer->dataRead)
1726
-+        {
1727
-+            remainingBytes = pStreamBuffer->dataRead;
1728
-+            VMREST_LOG_DEBUG(pRESTHandle,"WARNING: Requested %u bytes, available only %u bytes", bytesRequested,(dataAvailableInCache + remainingBytes));
1729
-+        }
1730
-+
1731
-+        memcpy((appBuffer + dataAvailableInCache),
1732
-+              &(pStreamBuffer->pData[dataIndex]),
1733
-+              remainingBytes);
1734
-+        pStreamBuffer->dataProcessed = remainingBytes;
1735
-+        *bytesRead = dataAvailableInCache + remainingBytes;
1736
-+
1737
-+        //VMREST_LOG_DEBUG(pRESTHandle,"dataAvailableInCache %u, remainingBytes %u, appBuffersize %u", dataAvailableInCache, remainingBytes, strlen(appBuffer));
1738
-+    }
1739
-+
1740
-+    VmwSockSetStreamBuffer( pRESTHandle, pSocket, pStreamBuffer);
1741
-+
1742
-+cleanup:
1743
-+    if (pIoBuffer)
1744
-+    {
1745
-+        VmwSockReleaseIoBuffer( pRESTHandle, pIoBuffer);
1746
-+    }
1747
-+
1748
-+    return dwError;
1749
-+
1750
-+error:
1751
-+
1752
-+    goto cleanup;
1753
-+
1754
-+}
1755
-+
1756
-+uint32_t
1757
-+VmSockPosixAdjustProcessedBytes(
1758
-+    PVMREST_HANDLE                   pRESTHandle,
1759
-+    PVM_SOCKET                       pSocket,
1760
-+    uint32_t                         dataSeen
1761
-+)
1762
-+{
1763
-+    uint32_t                         dwError = REST_ENGINE_SUCCESS;
1764
-+    PVM_STREAM_BUFFER                pStreamBuffer = NULL;
1765
-+
1766
-+    if (dataSeen > MAX_DATA_BUFFER_LEN)
1767
-+    {
1768
-+       VMREST_LOG_DEBUG(pRESTHandle,"%s","Invalid new Processed Data Index %u", dataSeen);
1769
-+        dwError = VMREST_TRANSPORT_INVALID_PARAM;
1770
-+    }
1771
-+    BAIL_ON_VMREST_ERROR(dwError);
1772
-+
1773
-+    VmwSockGetStreamBuffer( pRESTHandle, pSocket, &pStreamBuffer);
1774
-+
1775
-+    if (!pStreamBuffer)
1776
-+    {
1777
-+        dwError = 500;
1778
-+    }
1779
-+    BAIL_ON_VMREST_ERROR(dwError);
1780
-+
1781
-+    pStreamBuffer->dataProcessed = dataSeen;
1782
-+
1783
-+    VmwSockSetStreamBuffer( pRESTHandle, pSocket, pStreamBuffer);
1784
-+
1785
-+cleanup:
1786
-+    return dwError;
1787
-+error:
1788
-+    goto cleanup;
1789
-+}
1790
-+
1791
-+uint32_t
1792
-+VmSockPosixDecrementProcessedBytes(
1793
-+    PVMREST_HANDLE                   pRESTHandle,
1794
-+    PVM_SOCKET                       pSocket,
1795
-+    uint32_t                         offset
1796
-+)
1797
-+{
1798
-+    uint32_t                         dwError = REST_ENGINE_SUCCESS;
1799
-+    PVM_STREAM_BUFFER                pStreamBuffer = NULL;
1800
-+
1801
-+    if (offset > MAX_DATA_BUFFER_LEN)
1802
-+    {
1803
-+       VMREST_LOG_DEBUG(pRESTHandle,"%s","Invalid new Processed Data Index %u", offset);
1804
-+        dwError = VMREST_TRANSPORT_INVALID_PARAM;
1805
-+    }
1806
-+    BAIL_ON_VMREST_ERROR(dwError);
1807
-+
1808
-+    VmwSockGetStreamBuffer( pRESTHandle, pSocket, &pStreamBuffer);
1809
-+
1810
-+    if (!pStreamBuffer)
1811
-+    {
1812
-+        dwError = 500;
1813
-+    }
1814
-+    BAIL_ON_VMREST_ERROR(dwError);
1815
-+
1816
-+    if (pStreamBuffer->dataProcessed >= offset)
1817
-+    {
1818
-+        pStreamBuffer->dataProcessed = pStreamBuffer->dataProcessed - offset;
1819
-+    }
1820
-+    VmwSockSetStreamBuffer( pRESTHandle, pSocket, pStreamBuffer);
1821
-+
1822
-+cleanup:
1823
-+    return dwError;
1824
-+error:
1825
-+    goto cleanup;
1826
-+}
1827
-+
1828
-+
1829
-+uint32_t
1830
-+VmsockPosixWriteDataAtOnce(
1831
-+    PVMREST_HANDLE                   pRESTHandle,
1832
-+    PVM_SOCKET                       pSocket,
1833
-+    char*                            buffer,
1834
-+    uint32_t                         bytes
1835
-+    )
1836
-+{
1837
-+    uint32_t                         dwError = REST_ENGINE_SUCCESS;
1838
-+    PVM_SOCK_IO_BUFFER               pIoNewBuffer = NULL;
1839
-+    
1840
-+    dwError = VmwSockAllocateIoBuffer(
1841
-+                   pRESTHandle,
1842
-+                  VM_SOCK_EVENT_TYPE_TCP_RESPONSE_DATA_WRITE,
1843
-+                  bytes,
1844
-+                  &pIoNewBuffer);
1845
-+    BAIL_ON_VMREST_ERROR(dwError);
1846
-+
1847
-+    memcpy(pIoNewBuffer->pData, buffer, bytes);
1848
-+
1849
-+    dwError = VmwSockWrite(
1850
-+                   pRESTHandle,
1851
-+                  pSocket,
1852
-+                  NULL,
1853
-+                  0,
1854
-+                  pIoNewBuffer
1855
-+                  );
1856
-+    if (dwError == ERROR_SUCCESS)
1857
-+    {
1858
-+        dwError = REST_ENGINE_SUCCESS;
1859
-+        BAIL_ON_VMREST_ERROR(dwError);
1860
-+    }
1861
-+    else if (dwError == ERROR_IO_PENDING)
1862
-+    {
1863
-+        pIoNewBuffer = NULL;
1864
-+        BAIL_ON_VMREST_ERROR(dwError);
1865
-+    }
1866
-+
1867
-+cleanup:
1868
-+    if (pIoNewBuffer)
1869
-+    {
1870
-+        VmwSockReleaseIoBuffer( pRESTHandle, pIoNewBuffer);
1871
-+    }
1872
-+    return dwError;
1873
-+error:
1874
-+    goto cleanup;
1875
-+}
1876
-diff -ru c-rest-engine-1.0.3/include/public/vmrest.h c-rest-engine-1.0.4/include/public/vmrest.h
1877
-+++ c-rest-engine-1.0.4/include/public/vmrest.h	2017-08-21 16:40:55.000000000 -0700
1878
-@@ -41,6 +41,7 @@
1879
- #define     REST_ERROR_BAD_CONFIG_FILE_PATH                110
1880
- #define     REST_ERROR_PREV_INSTANCE_NOT_CLEAN             111
1881
- #define     REST_ERROR_INVALID_HANDLER                     112
1882
-+#define     REST_ENGINE_SSL_CONFIG_FILE                    113
1883
- #define     REST_ENGINE_MORE_IO_REQUIRED                   7001
1884
- #define     REST_ENGINE_IO_COMPLETED                       0
1885
- 
1886
-diff -ru c-rest-engine-1.0.3/server/restengine/defines.h c-rest-engine-1.0.4/server/restengine/defines.h
1887
-+++ c-rest-engine-1.0.4/server/restengine/defines.h	2017-08-21 16:40:55.000000000 -0700
1888
-@@ -25,6 +25,7 @@
1889
- #define HTTP_VER_LEN                8
1890
- #define HTTP_CHUNKED_DATA_LEN       7
1891
- #define HTTP_MIN_CHUNK_DATA_LEN     3
1892
-+#define HTTP_CRLF_LEN               2
1893
- 
1894
- #define MAX_KEY_VAL_PARAM_LEN      1024
1895
- #define MAX_URL_PARAMS_ARR_SIZE    5
1896
-diff -ru c-rest-engine-1.0.3/server/restengine/httpProtocolHead.c c-rest-engine-1.0.4/server/restengine/httpProtocolHead.c
1897
-+++ c-rest-engine-1.0.4/server/restengine/httpProtocolHead.c	2017-08-21 16:40:55.000000000 -0700
1898
-@@ -234,7 +234,7 @@
1899
- {
1900
-     uint32_t                         dwError = REST_ENGINE_SUCCESS;
1901
-     char*                            buffer = NULL;
1902
--    char                             local[MAX_REQ_LIN_LEN] = {0};
1903
-+    char*                            local = NULL;
1904
-     char                             attribute[MAX_HTTP_HEADER_ATTR_LEN] = {0};
1905
-     char                             value[MAX_HTTP_HEADER_VAL_LEN] = {0};
1906
-     char*                            temp = NULL;
1907
-@@ -242,6 +242,12 @@
1908
-     size_t                           attrLen = 0;
1909
-     size_t                           valLen  = 0;
1910
- 
1911
-+    dwError = VmRESTAllocateMemory(
1912
-+                  MAX_REQ_LIN_LEN,
1913
-+                  (void**)&local
1914
-+                  );
1915
-+    BAIL_ON_VMREST_ERROR(dwError);
1916
-+
1917
-     buffer = line;
1918
-     temp = local;
1919
- 
1920
-@@ -263,7 +269,7 @@
1921
-             *temp = '\0';
1922
-             strncpy(attribute,local,(MAX_HTTP_HEADER_ATTR_LEN - 1));
1923
-             attrLen = strlen(attribute);
1924
--            memset(local,'\0', sizeof(local));
1925
-+            memset(local,'\0', MAX_REQ_LIN_LEN);
1926
-             temp = local;
1927
-             continue;
1928
-         }
1929
-@@ -293,6 +299,12 @@
1930
-     BAIL_ON_VMREST_ERROR(dwError);
1931
- 
1932
- cleanup:
1933
-+    if (local != NULL)
1934
-+    {
1935
-+        VmRESTFreeMemory(local);
1936
-+        local = NULL;
1937
-+    }
1938
-+
1939
-     return dwError;
1940
- error:
1941
-     goto cleanup;
1942
-@@ -310,7 +322,7 @@
1943
- {
1944
-     uint32_t                         dwError = REST_ENGINE_SUCCESS;
1945
-     char                             method[MAX_METHOD_LEN] = {0};
1946
--    char                             URI[MAX_URI_LEN]={0};
1947
-+    char*                            URI = NULL;
1948
-     char                             version[MAX_VERSION_LEN] = {0};
1949
- 
1950
-     if (!line  || !pReqPacket || (*resStatus != OK) || lineNo == 0)
1951
-@@ -320,6 +332,12 @@
1952
-     }
1953
-     BAIL_ON_VMREST_ERROR(dwError);
1954
- 
1955
-+    dwError = VmRESTAllocateMemory(
1956
-+                  MAX_URI_LEN,
1957
-+                  (PVOID*)&URI
1958
-+                  );
1959
-+    BAIL_ON_VMREST_ERROR(dwError);
1960
-+
1961
-     if (lineLen > MAX_REQ_LIN_LEN)
1962
-     {
1963
-         dwError = REQUEST_URI_TOO_LARGE;
1964
-@@ -337,7 +355,7 @@
1965
-                       resStatus
1966
-                       );
1967
-         BAIL_ON_VMREST_ERROR(dwError);
1968
--        strcpy(pReqPacket->requestLine->method, method);
1969
-+        strncpy(pReqPacket->requestLine->method, method, (MAX_METHOD_LEN - 1));
1970
- 
1971
-         dwError = VmRESTHTTPGetReqURI(
1972
-                       line,
1973
-@@ -346,7 +364,7 @@
1974
-                       resStatus
1975
-                       );
1976
-         BAIL_ON_VMREST_ERROR(dwError);
1977
--        strcpy(pReqPacket->requestLine->uri, URI);
1978
-+        strncpy(pReqPacket->requestLine->uri, URI, (MAX_URI_LEN - 1));
1979
- 
1980
-         dwError = VmRESTHTTPGetReqVersion(
1981
-                       line,
1982
-@@ -355,7 +373,7 @@
1983
-                       resStatus
1984
-                       );
1985
-         BAIL_ON_VMREST_ERROR(dwError);
1986
--        strcpy(pReqPacket->requestLine->version, version);
1987
-+        strncpy(pReqPacket->requestLine->version, version, (MAX_VERSION_LEN - 1));
1988
-     }
1989
-     else
1990
-     {
1991
-@@ -370,6 +388,11 @@
1992
-     }
1993
- 
1994
- cleanup:
1995
-+    if (URI != NULL)
1996
-+    {
1997
-+        VmRESTFreeMemory(URI);
1998
-+        URI = NULL;
1999
-+    }
2000
-     return dwError;
2001
- error:
2002
-     goto cleanup;
2003
-@@ -384,123 +407,161 @@
2004
-     uint32_t*                        resStatus
2005
-     )
2006
- {
2007
-+    int                              nDataStart = 0;
2008
-+    char                             sockBuffer[MAX_DATA_BUFFER_LEN]={0};
2009
-+    char*                            local = NULL;
2010
-+    char*                            pszStartNewLine = buffer;
2011
-+    char*                            pszEndNewLine = NULL;
2012
-+    char*                            newBuf = NULL;
2013
-+    char*                            prevBuffer = NULL;
2014
-+    char*                            prevBufferTemp = NULL;
2015
-     uint32_t                         dwError = REST_ENGINE_SUCCESS;
2016
--    uint32_t                         bytesRead = 0;
2017
-+    uint32_t                         nProcessed = 0;
2018
-     uint32_t                         lineNo = 0;
2019
--    size_t                           lineLen = 0;
2020
--    char                             local[MAX_REQ_LIN_LEN]={0};
2021
--    char*                            temp = buffer;
2022
--    char*                            line = local;
2023
--    char                             appBuffer[MAX_DATA_BUFFER_LEN]={0};
2024
--    uint32_t                         bytesReadInBuffer = 0;
2025
--    uint32_t                         skipRead = 0;
2026
--    uint32_t                         extraBytes = 0;
2027
-+    uint32_t                         nPrevBuf = 0;
2028
-+    uint32_t                         nLineLen = 0;
2029
-+    uint32_t                         nBufLen = packetLen;
2030
-+    uint32_t                         nEmptyPrevBuf = 0;
2031
-+    uint32_t                         nSockReadRequest = 0;
2032
-+    uint32_t                         nSockReadActual = 0;
2033
-+
2034
- 
2035
--    if (!buffer || !pReqPacket || (*resStatus != OK) || (packetLen <= 4))
2036
-+    if (!buffer || !pReqPacket || (*resStatus != OK))
2037
-     {
2038
-        VMREST_LOG_ERROR(pRESTHandle,"%s","Invalid params");
2039
-        dwError =  BAD_REQUEST;
2040
-        *resStatus = BAD_REQUEST;
2041
-     }
2042
-     BAIL_ON_VMREST_ERROR(dwError);
2043
--    
2044
--    while(1)
2045
--    {
2046
--        if (bytesRead >= (packetLen - 4))
2047
--        {
2048
- 
2049
--            if (temp && (strcmp(temp,"\r\n\r\n") == 0))
2050
--            {
2051
--                skipRead = 1;
2052
--            }
2053
--            /**** More socket read required to process the headers ****/
2054
-+    /**** Allocate all memory for buffers ****/
2055
-+    dwError = VmRESTAllocateMemory(
2056
-+                  MAX_REQ_LIN_LEN,
2057
-+                  (PVOID*)&local
2058
-+                  );
2059
-+    BAIL_ON_VMREST_ERROR(dwError);
2060
-+
2061
-+    dwError = VmRESTAllocateMemory(
2062
-+                  MAX_REQ_LIN_LEN,
2063
-+                  (PVOID*)&prevBuffer
2064
-+                  );
2065
-+    BAIL_ON_VMREST_ERROR(dwError);
2066
-+
2067
-+    dwError = VmRESTAllocateMemory(
2068
-+                  MAX_REQ_LIN_LEN,
2069
-+                  (PVOID*)&prevBufferTemp
2070
-+                  );
2071
-+    BAIL_ON_VMREST_ERROR(dwError);
2072
- 
2073
--            if (!skipRead)
2074
-+    while(pszStartNewLine != NULL)
2075
-+    {
2076
-+        pszEndNewLine = strstr(pszStartNewLine, "\r\n");
2077
-+        if (pszEndNewLine != NULL)
2078
-+        {
2079
-+            nLineLen = pszEndNewLine - pszStartNewLine;
2080
-+            VMREST_LOG_DEBUG(pRESTHandle,"Line length is: %u",nLineLen);
2081
-+            if( nLineLen == 0 )
2082
-             {
2083
--                extraBytes = packetLen - bytesRead;
2084
--                dwError = VmSockPosixAdjustProcessedBytes(
2085
--                              pRESTHandle,
2086
--                              pReqPacket->pSocket,
2087
--                              bytesRead
2088
--                              );
2089
-+                /**** This is the end of all HTTP headers ****/
2090
-+                nDataStart = nProcessed - nPrevBuf + HTTP_CRLF_LEN;
2091
-+                if ( nDataStart < 0 )
2092
-+                {
2093
-+                    VMREST_LOG_ERROR(pRESTHandle,"Bad request detected, Negative data start postion: %d", nDataStart);
2094
-+                    dwError = BAD_REQUEST;
2095
-+                }
2096
-                 BAIL_ON_VMREST_ERROR(dwError);
2097
--                memset(appBuffer, '\0', MAX_DATA_BUFFER_LEN);
2098
- 
2099
--                dwError = VmsockPosixGetXBytes(
2100
-+                dwError = VmSockPosixAdjustProcessedBytes(
2101
-                               pRESTHandle,
2102
--                              MAX_DATA_BUFFER_LEN,
2103
--                              appBuffer,
2104
-                               pReqPacket->pSocket,
2105
--                              &bytesReadInBuffer,
2106
--                              1
2107
-+                              (uint32_t)nDataStart
2108
-                               );
2109
-                 BAIL_ON_VMREST_ERROR(dwError);
2110
--                temp = appBuffer;
2111
--                bytesRead = 0;
2112
--                packetLen = bytesReadInBuffer;
2113
--
2114
--                if ((packetLen <= 4) && (strcmp(appBuffer, "\r\n\r\n") != 0))
2115
--                {
2116
--                    skipRead = 1;
2117
--                    VMREST_LOG_ERROR(pRESTHandle,"%s","Bad HTTP request detected");
2118
--                    dwError =  VMREST_HTTP_VALIDATION_FAILED;
2119
--                    *resStatus = BAD_REQUEST;
2120
--                }
2121
--                BAIL_ON_VMREST_ERROR(dwError);
2122
-+                VMREST_LOG_DEBUG(pRESTHandle,"Finished headers parsing with nProcessed %u", nProcessed);
2123
-+                break;
2124
-             }
2125
--        }
2126
--        if((*temp == '\r') && (*(temp+1) == '\n'))
2127
--        {
2128
-+            strncpy(local, pszStartNewLine, nLineLen);
2129
-             lineNo++;
2130
--            *line = '\0';
2131
--            lineLen = strlen(local);
2132
--            /* call handler function with reqLine */
2133
-+
2134
-+            /**** Found a new line, process it and store in HTTP request pcaket ****/
2135
-             dwError = VmRESTParseHTTPReqLine(
2136
-                           lineNo,
2137
-                           local,
2138
--                          (uint32_t)lineLen,
2139
-+                          nLineLen,
2140
-                           pReqPacket,
2141
-                           resStatus
2142
-                           );
2143
-             BAIL_ON_VMREST_ERROR(dwError);
2144
--            bytesRead = bytesRead + 2;
2145
--            if((*(temp+2) == '\r') && (*(temp+3) == '\n'))
2146
--            {
2147
--                bytesRead = bytesRead + 2;
2148
--                VMREST_LOG_DEBUG(pRESTHandle,"Finished headers parsing with bytesRead %u", bytesRead);
2149
--                /**** All headers processed : data starts from here ***/
2150
--                dwError = VmSockPosixAdjustProcessedBytes(
2151
--                              pRESTHandle,
2152
--                              pReqPacket->pSocket,
2153
--                              (bytesRead - extraBytes)
2154
--                              );
2155
--                BAIL_ON_VMREST_ERROR(dwError);
2156
--                break;
2157
--            }
2158
--            temp = temp + 2;
2159
-             memset(local, '\0', MAX_REQ_LIN_LEN);
2160
--            line = local;
2161
--            continue;
2162
--        }
2163
--        if ((line - local) < MAX_REQ_LIN_LEN )
2164
--        {
2165
--            *line = *temp;
2166
--            temp++;
2167
--            line++;
2168
--            bytesRead++;
2169
-+            nProcessed = nProcessed + nLineLen + HTTP_CRLF_LEN;
2170
-+            pszStartNewLine = pszEndNewLine + HTTP_CRLF_LEN;
2171
-+            pszEndNewLine = NULL;
2172
-         }
2173
--        else
2174
-+        else /**** pszEndNewLine == NULL ****/
2175
-         {
2176
--            dwError = REQUEST_HEADER_FIELD_TOO_LARGE;
2177
-+            /**** more socket reads will be required to process the headers ****/
2178
-+            nPrevBuf = nBufLen - nProcessed;
2179
-+            if (nPrevBuf >= MAX_REQ_LIN_LEN)
2180
-+            {
2181
-+                dwError = REQUEST_URI_TOO_LARGE;
2182
-+                VMREST_LOG_ERROR(pRESTHandle,"%s","Too large URI");
2183
-+            }
2184
-             BAIL_ON_VMREST_ERROR(dwError);
2185
-+
2186
-+            if (nProcessed != 0)
2187
-+            {
2188
-+                /**** Last socket read had CRLF, so adjust the remaining bytes only ****/
2189
-+                memset(prevBufferTemp, '\0', MAX_REQ_LIN_LEN);
2190
-+                strncpy(prevBufferTemp, pszStartNewLine, nPrevBuf);
2191
-+                pszStartNewLine  = prevBufferTemp;
2192
-+                memset(prevBuffer, '\0', MAX_REQ_LIN_LEN);
2193
-+            }
2194
-+
2195
-+            strncpy(prevBuffer, pszStartNewLine, nPrevBuf);
2196
-+            VMREST_LOG_DEBUG(pRESTHandle,"Requesting read from socket layer, still processing headers .....");
2197
-+            memset(sockBuffer, '\0', MAX_DATA_BUFFER_LEN);
2198
-+
2199
-+            nEmptyPrevBuf = MAX_REQ_LIN_LEN - nPrevBuf;
2200
-+            nSockReadRequest = ((nEmptyPrevBuf > MAX_DATA_BUFFER_LEN) ? MAX_DATA_BUFFER_LEN : nEmptyPrevBuf);
2201
-+
2202
-+            dwError = VmsockPosixGetXBytes(
2203
-+                          pRESTHandle,
2204
-+                          nSockReadRequest,
2205
-+                          sockBuffer,
2206
-+                          pReqPacket->pSocket,
2207
-+                          &nSockReadActual,
2208
-+                          1
2209
-+                          );
2210
-+            BAIL_ON_VMREST_ERROR(dwError);
2211
-+
2212
-+            newBuf = strncat(prevBuffer, sockBuffer, nSockReadActual);
2213
-+            pszStartNewLine = newBuf;
2214
-+            nBufLen = nSockReadActual + nPrevBuf;
2215
-+            nProcessed = 0;
2216
-         }
2217
-     }
2218
-+
2219
- cleanup:
2220
-+    if (local != NULL)
2221
-+    {
2222
-+        VmRESTFreeMemory(local);
2223
-+        local = NULL;
2224
-+    }
2225
-+    if (prevBuffer != NULL)
2226
-+    {
2227
-+        VmRESTFreeMemory(prevBuffer);
2228
-+        prevBuffer = NULL;
2229
-+    }
2230
-+    if (prevBufferTemp != NULL)
2231
-+    {
2232
-+        VmRESTFreeMemory(prevBufferTemp);
2233
-+        prevBufferTemp = NULL;
2234
-+    }
2235
-+
2236
-     return dwError;
2237
- error:
2238
-     goto cleanup;
2239
- }
2240
--
2241
- uint32_t
2242
- VMRESTWriteChunkedMessageInResponseStream(
2243
-     char*                            src,
2244
-@@ -1087,8 +1148,8 @@
2245
-     char*                            transferEncoding = NULL;
2246
-     char*                            expect = NULL;
2247
-     uint32_t                         done = 0;
2248
--    char                             httpURI[MAX_URI_LEN] = {0};
2249
--    char                             endPointURI[MAX_URI_LEN] = {0};
2250
-+    char*                            httpURI = NULL;
2251
-+    char*                            endPointURI = NULL;
2252
-     char*                            ptr = NULL;
2253
-     PREST_ENDPOINT                   pEndPoint = NULL;
2254
- 
2255
-@@ -1111,6 +1172,18 @@
2256
-                   );
2257
-     BAIL_ON_VMREST_ERROR(dwError);
2258
- 
2259
-+    dwError = VmRESTAllocateMemory(
2260
-+                  MAX_URI_LEN,
2261
-+                  (PVOID*)&httpURI
2262
-+                  );
2263
-+    BAIL_ON_VMREST_ERROR(dwError);
2264
-+
2265
-+    dwError = VmRESTAllocateMemory(
2266
-+                  MAX_URI_LEN,
2267
-+                  (PVOID*)&endPointURI
2268
-+                  );
2269
-+    BAIL_ON_VMREST_ERROR(dwError);
2270
-+
2271
-     pReqPacket->miscHeader->head = NULL;
2272
-     pResPacket->miscHeader->head = NULL;
2273
-     pReqPacket->pSocket = pSocket;
2274
-@@ -1308,6 +1381,19 @@
2275
-         /**** Error response is already sent to client, return success ****/
2276
-         dwError = REST_ENGINE_SUCCESS;
2277
-     }
2278
-+
2279
-+    if (httpURI != NULL)
2280
-+    {
2281
-+        VmRESTFreeMemory(httpURI);
2282
-+        httpURI = NULL;
2283
-+    }
2284
-+
2285
-+    if (endPointURI != NULL)
2286
-+    {
2287
-+        VmRESTFreeMemory(endPointURI);
2288
-+        endPointURI = NULL;
2289
-+    }
2290
-+
2291
-     return dwError;
2292
- error:
2293
-     VMREST_LOG_ERROR(pRESTHandle,"Something failed, dwError = %u", dwError);
2294
-diff -ru c-rest-engine-1.0.3/server/restengine/httpUtilsInternal.c c-rest-engine-1.0.4/server/restengine/httpUtilsInternal.c
2295
-+++ c-rest-engine-1.0.4/server/restengine/httpUtilsInternal.c	2017-08-21 16:40:55.000000000 -0700
2296
-@@ -484,7 +484,7 @@
2297
-     }
2298
-     BAIL_ON_VMREST_ERROR(dwError);
2299
- 
2300
--    strncpy(portNo, pRESTConfig->server_port,MAX_SERVER_PORT_LEN);
2301
-+    strncpy(portNo, pRESTConfig->server_port,(MAX_SERVER_PORT_LEN -1));
2302
- 
2303
-     lastPortChar = VmRESTUtilsGetLastChar(
2304
-                        pRESTConfig->server_port
2305
-diff -ru c-rest-engine-1.0.3/server/restengine/libmain.c c-rest-engine-1.0.4/server/restengine/libmain.c
2306
-+++ c-rest-engine-1.0.4/server/restengine/libmain.c	2017-08-21 16:40:55.000000000 -0700
2307
-@@ -100,7 +100,7 @@
2308
-      uint32_t                         dwError = REST_ENGINE_SUCCESS;
2309
-      char                             fileName[MAX_PATH_LEN] = {0};
2310
-      FILE*                            fp = NULL;
2311
--     int                              writtenBytes = 0;
2312
-+     uint32_t                         writtenBytes = 0;
2313
- 
2314
-     if (!pRESTHandle || !pDataBuffer || (bufferSize == 0) || (bufferSize > MAX_SSL_DATA_BUF_LEN) || (sslDataType < SSL_DATA_TYPE_KEY) || (sslDataType > SSL_DATA_TYPE_CERT))
2315
-     {
2316
-@@ -110,42 +110,42 @@
2317
- 
2318
-     if (((pRESTHandle->pSSLInfo->isCertSet != SSL_INFO_NOT_SET) && (sslDataType == SSL_DATA_TYPE_CERT)))
2319
-     {
2320
--        goto cleanup;
2321
-+        dwError = REST_ENGINE_SSL_CONFIG_FILE;
2322
-     }
2323
-+    BAIL_ON_VMREST_ERROR(dwError);
2324
- 
2325
-     if (((pRESTHandle->pSSLInfo->isKeySet != SSL_INFO_NOT_SET) && (sslDataType == SSL_DATA_TYPE_KEY)))
2326
-     {
2327
--        goto cleanup;
2328
-+        dwError = REST_ENGINE_SSL_CONFIG_FILE;
2329
-     }
2330
--    memset(fileName, '\0', MAX_PATH_LEN);
2331
-+    BAIL_ON_VMREST_ERROR(dwError);
2332
- 
2333
-     if (sslDataType == SSL_DATA_TYPE_KEY)
2334
-     {
2335
--        sprintf(fileName, "%s%p.pem","/tmp/key-", pRESTHandle);
2336
-+        snprintf(fileName, (MAX_PATH_LEN - 1),"%s%s.pem", "/tmp/key-port-", pRESTHandle->pRESTConfig->server_port);
2337
- 
2338
-     }
2339
-     else if (sslDataType == SSL_DATA_TYPE_CERT)
2340
-     {
2341
--        sprintf(fileName, "%s%p.pem","/tmp/cert-", pRESTHandle);
2342
-+        snprintf(fileName, (MAX_PATH_LEN - 1), "%s%s.pem", "/tmp/cert-port-", pRESTHandle->pRESTConfig->server_port);
2343
-     }
2344
- 
2345
-     fp = fopen(fileName, "w+");
2346
--
2347
-     if (fp == NULL)
2348
-     {
2349
-         VMREST_LOG_ERROR(pRESTHandle,"Unable to Open SSL file %s", fileName);
2350
-         dwError = REST_ENGINE_FAILURE;
2351
-     }
2352
-+    BAIL_ON_VMREST_ERROR(dwError);
2353
- 
2354
-     writtenBytes = fwrite(pDataBuffer, 1, bufferSize, fp);
2355
-+    fclose(fp);
2356
-     
2357
-     if (writtenBytes != bufferSize)
2358
-     {
2359
--        VMREST_LOG_WARNING(pRESTHandle,"Not all buffer bytes written to file, requested %u, written %d", bufferSize, writtenBytes);
2360
-+        VMREST_LOG_WARNING(pRESTHandle,"Not all buffer bytes written to file, requested %u, written %u", bufferSize, writtenBytes);
2361
-     }
2362
- 
2363
--    fclose(fp);
2364
--
2365
-     if (sslDataType == SSL_DATA_TYPE_KEY)
2366
-     {
2367
-         memset(pRESTHandle->pRESTConfig->ssl_key, '\0', MAX_PATH_LEN);
2368
-@@ -177,6 +177,7 @@
2369
-     )
2370
- {
2371
-     uint32_t                         dwError = REST_ENGINE_SUCCESS;
2372
-+    int                              ret = 0;
2373
- 
2374
-     if (!pRESTHandle)
2375
-     {
2376
-@@ -200,11 +201,23 @@
2377
- 
2378
-     if (pRESTHandle->pSSLInfo->isCertSet == SSL_INFO_FROM_BUFFER_API)
2379
-     {
2380
--        remove(pRESTHandle->pRESTConfig->ssl_certificate);
2381
-+        if ((ret = remove(pRESTHandle->pRESTConfig->ssl_certificate)) == -1)
2382
-+        {
2383
-+            VMREST_LOG_ERROR(pRESTHandle, "remove() syscall failed for temp certificate file ()");
2384
-+            dwError = REST_ENGINE_FAILURE;
2385
-+        }
2386
-+        BAIL_ON_VMREST_ERROR(dwError);
2387
-     }
2388
-+    ret = 0;
2389
-+
2390
-     if (pRESTHandle->pSSLInfo->isKeySet == SSL_INFO_FROM_BUFFER_API)
2391
-     {
2392
--        remove(pRESTHandle->pRESTConfig->ssl_key);
2393
-+        if ((ret = remove(pRESTHandle->pRESTConfig->ssl_key)) == -1)
2394
-+        {
2395
-+            VMREST_LOG_ERROR(pRESTHandle, "remove temp file failed ()");
2396
-+            dwError = REST_ENGINE_FAILURE;
2397
-+        }
2398
-+        BAIL_ON_VMREST_ERROR(dwError);
2399
-     }
2400
- 
2401
- cleanup:
2402
-diff -ru c-rest-engine-1.0.3/server/restengine/restProtocolHead.c c-rest-engine-1.0.4/server/restengine/restProtocolHead.c
2403
-+++ c-rest-engine-1.0.4/server/restengine/restProtocolHead.c	2017-08-21 16:40:55.000000000 -0700
2404
-@@ -20,10 +20,9 @@
2405
-     PREST_RESPONSE*                  ppResponse
2406
-     )
2407
- {
2408
--    char                             httpPayload[MAX_DATA_BUFFER_LEN] = {0};
2409
-     char                             httpMethod[MAX_METHOD_LEN] = {0};
2410
--    char                             httpURI[MAX_URI_LEN] = {0};
2411
--    char                             endPointURI[MAX_URI_LEN] = {0};
2412
-+    char*                            httpURI = NULL;
2413
-+    char*                            endPointURI = NULL;
2414
-     char*                            ptr = NULL;
2415
-     uint32_t                         dwError = REST_ENGINE_SUCCESS;
2416
-     uint32_t                         paramsCount = 0;
2417
-@@ -31,12 +30,19 @@
2418
- 
2419
-     VMREST_LOG_DEBUG(pRESTHandle,"%s","Internal Handler called");
2420
- 
2421
--    /**** 1. Init all the funcition variables *****/
2422
-+    /**** 1. Allocate the memory of holding URI *****/
2423
- 
2424
--    memset(httpPayload, '\0', MAX_DATA_BUFFER_LEN);
2425
--    memset(httpMethod, '\0', MAX_METHOD_LEN);
2426
--    memset(httpURI, '\0', MAX_URI_LEN);
2427
--    memset(endPointURI, '\0', MAX_URI_LEN);
2428
-+    dwError = VmRESTAllocateMemory(
2429
-+                  MAX_URI_LEN,
2430
-+                  (void**)&endPointURI
2431
-+                  );
2432
-+    BAIL_ON_VMREST_ERROR(dwError);
2433
-+
2434
-+    dwError = VmRESTAllocateMemory(
2435
-+                  MAX_URI_LEN,
2436
-+                  (void**)&httpURI
2437
-+                  );
2438
-+    BAIL_ON_VMREST_ERROR(dwError);
2439
- 
2440
-     /**** 2. Get the method name ****/
2441
- 
2442
-@@ -191,6 +197,16 @@
2443
-     BAIL_ON_VMREST_ERROR(dwError);
2444
- 
2445
- cleanup:
2446
-+    if (endPointURI != NULL)
2447
-+    {
2448
-+        VmRESTFreeMemory(endPointURI);
2449
-+        endPointURI = NULL;
2450
-+    }
2451
-+    if (httpURI != NULL)
2452
-+    {
2453
-+        VmRESTFreeMemory(httpURI);
2454
-+        httpURI = NULL;
2455
-+    }
2456
-     return dwError;
2457
- error:
2458
-     goto cleanup;
2459
-@@ -645,11 +661,17 @@
2460
-                     {
2461
-                         strncpy(res, (value +1), (MAX_KEY_VAL_PARAM_LEN -1));
2462
-                     }
2463
-+                    else if ((*(value +1) == '\0'))
2464
-+                    {
2465
-+                        VMREST_LOG_DEBUG(pRESTHandle, "Missing value in key-value pair");
2466
-+                        memset(res, '\0', MAX_KEY_VAL_PARAM_LEN);
2467
-+                    }
2468
-                     else
2469
-                     {
2470
-+                        VMREST_LOG_ERROR(pRESTHandle, "Value too large");
2471
-                         dwError = REQUEST_ENTITY_TOO_LARGE;
2472
-                     }
2473
--                }    
2474
-+                } 
2475
-             }
2476
-             else
2477
-             {
2478
-@@ -926,8 +948,8 @@
2479
-     uint32_t*                        wildCardCount
2480
-     )
2481
- {
2482
--    char                             httpURI[MAX_URI_LEN] = {0};
2483
--    char                             endPointURI[MAX_URI_LEN] = {0};
2484
-+    char*                            httpURI = NULL;
2485
-+    char*                            endPointURI = NULL;
2486
-     char*                            ptr = NULL;
2487
-     uint32_t                         dwError = REST_ENGINE_SUCCESS;
2488
-     uint32_t                         count = 0;
2489
-@@ -941,8 +963,17 @@
2490
-     BAIL_ON_VMREST_ERROR(dwError);
2491
-     *wildCardCount = 0;
2492
- 
2493
--    memset(httpURI, '\0', MAX_URI_LEN);
2494
--    memset(endPointURI, '\0', MAX_URI_LEN);
2495
-+    dwError = VmRESTAllocateMemory(
2496
-+                  MAX_URI_LEN,
2497
-+                  (void**)&endPointURI
2498
-+                  );
2499
-+    BAIL_ON_VMREST_ERROR(dwError);
2500
-+
2501
-+    dwError = VmRESTAllocateMemory(
2502
-+                  MAX_URI_LEN,
2503
-+                  (void**)&httpURI
2504
-+                  );
2505
-+    BAIL_ON_VMREST_ERROR(dwError);
2506
- 
2507
-     dwError = VmRESTGetHttpURI(
2508
-                   pRequest,
2509
-@@ -989,6 +1020,17 @@
2510
-     *wildCardCount = count;
2511
- 
2512
- cleanup:
2513
-+    if (endPointURI != NULL)
2514
-+    {
2515
-+        VmRESTFreeMemory(endPointURI);
2516
-+        endPointURI = NULL;
2517
-+    }
2518
-+    if (httpURI != NULL)
2519
-+    {
2520
-+        VmRESTFreeMemory(httpURI);
2521
-+        httpURI = NULL;
2522
-+    }
2523
-+
2524
-     return dwError;
2525
- error:
2526
-     if (wildCardCount != NULL)
2527
-@@ -1010,8 +1052,8 @@
2528
-     uint32_t                         dwError = REST_ENGINE_SUCCESS;
2529
-     uint32_t                         count = 0;
2530
-     uint32_t                         preSlashIndex = 0;
2531
--    char                             httpURI[MAX_URI_LEN] = {0};
2532
--    char                             endPointURI[MAX_URI_LEN] = {0};
2533
-+    char*                            httpURI = NULL;
2534
-+    char*                            endPointURI = NULL;
2535
-     char*                            ptr = NULL;
2536
-     char*                            pszWildCard = NULL;
2537
-     PREST_ENDPOINT                   pEndPoint = NULL;
2538
-@@ -1023,6 +1065,18 @@
2539
-     }
2540
-     BAIL_ON_VMREST_ERROR(dwError);
2541
- 
2542
-+    dwError = VmRESTAllocateMemory(
2543
-+                  MAX_URI_LEN,
2544
-+                  (void**)&endPointURI
2545
-+                  );
2546
-+    BAIL_ON_VMREST_ERROR(dwError);
2547
-+
2548
-+    dwError = VmRESTAllocateMemory(
2549
-+                  MAX_URI_LEN,
2550
-+                  (void**)&httpURI
2551
-+                  );
2552
-+    BAIL_ON_VMREST_ERROR(dwError);
2553
-+
2554
-     dwError = VmRESTGetWildCardCount(
2555
-                   pRESTHandle,
2556
-                   pRequest,
2557
-@@ -1097,6 +1151,16 @@
2558
- 
2559
- 
2560
- cleanup:
2561
-+    if (endPointURI != NULL)
2562
-+    {
2563
-+        VmRESTFreeMemory(endPointURI);
2564
-+        endPointURI = NULL;
2565
-+    }
2566
-+    if (httpURI != NULL)
2567
-+    {
2568
-+        VmRESTFreeMemory(httpURI);
2569
-+        httpURI = NULL;
2570
-+    }
2571
-     return dwError;
2572
- error:
2573
-     if (pszWildCard)
2574
-diff -ru c-rest-engine-1.0.3/server/vmrestd/main.c c-rest-engine-1.0.4/server/vmrestd/main.c
2575
-+++ c-rest-engine-1.0.4/server/vmrestd/main.c	2017-08-21 16:40:55.000000000 -0700
2576
-@@ -81,6 +81,18 @@
2577
-     dwError = VmRESTInit(NULL,configPath, &gpRESTHandle);
2578
-     dwError = VmRESTInit(NULL,configPath1, &gpRESTHandle1);
2579
- 
2580
-+// test set SSL info API
2581
-+#if 0 
2582
-+   char buffer[8092]= {0};
2583
-+   FILE *fp = fopen("/root/mycert.pem","r");
2584
-+
2585
-+   dwError = fread(buffer, 1, 8092, fp);
2586
-+   VmRESTSetSSLInfo(gpRESTHandle, buffer, dwError, SSL_DATA_TYPE_KEY);
2587
-+   VmRESTSetSSLInfo(gpRESTHandle, buffer, dwError, SSL_DATA_TYPE_CERT);
2588
-+
2589
-+
2590
-+#endif
2591
-+
2592
-     VmRESTRegisterHandler(gpRESTHandle, "/v1/pkg", &gVmRestHandlers, NULL);
2593
-     VmRESTRegisterHandler(gpRESTHandle1, "/v1/blah", &gVmRestHandlers1, NULL);
2594
- 
2595
-@@ -121,7 +133,7 @@
2596
-     )
2597
- {
2598
-     uint32_t                         dwError = REST_ENGINE_MORE_IO_REQUIRED;
2599
--    char                             AllData[MAX_IN_MEM_PAYLOAD_LEN] = {0};
2600
-+    char*                            AllData = NULL;
2601
-     char                             buffer[4097] = {0};
2602
-     int                              nRead = 0;
2603
-     int                              nWrite = 0;
2604
-@@ -131,9 +143,11 @@
2605
-     int                              resLength = 0;
2606
-     uint32_t                         index = 0;
2607
-     
2608
--    memset(AllData, '\0', MAX_IN_MEM_PAYLOAD_LEN);
2609
-     memset(size, '\0', 10);
2610
- 
2611
-+    AllData = malloc(MAX_IN_MEM_PAYLOAD_LEN);
2612
-+    memset(AllData, '\0', MAX_IN_MEM_PAYLOAD_LEN);
2613
-+
2614
-     bytesRW = 0;
2615
-     
2616
-     while(dwError == REST_ENGINE_MORE_IO_REQUIRED)
2617
-@@ -230,6 +244,12 @@
2618
-     BAIL_ON_VMREST_ERROR(dwError);
2619
- 
2620
- cleanup:
2621
-+    if (AllData != NULL)
2622
-+    {
2623
-+        free(AllData);
2624
-+        AllData = NULL;
2625
-+    }
2626
-+
2627
-     return dwError;
2628
- error:
2629
-     goto cleanup;
2630
-@@ -247,7 +267,7 @@
2631
-     )
2632
- {
2633
-     uint32_t                         dwError = 0;
2634
--    char                             AllData[MAX_IN_MEM_PAYLOAD_LEN] = {0};
2635
-+    char*                            AllData = NULL;
2636
-     char                             buffer[4097] = {0};
2637
-     int                              nRead = 0;
2638
-     int                              nWrite = 0;
2639
-@@ -259,9 +279,11 @@
2640
-     FILE*                            fp = NULL;
2641
- 
2642
-     memset(buffer, '\0', 4097);
2643
--    memset(AllData, '\0', MAX_IN_MEM_PAYLOAD_LEN);
2644
-     memset(size, '\0', 10);
2645
- 
2646
-+    AllData = malloc(MAX_IN_MEM_PAYLOAD_LEN);
2647
-+    memset(AllData, '\0', MAX_IN_MEM_PAYLOAD_LEN);
2648
-+
2649
-     dwError = REST_ENGINE_MORE_IO_REQUIRED;
2650
- 
2651
-     fp = fopen("/tmp/image.vmdk","wb+");
2652
-@@ -303,6 +325,7 @@
2653
-     BAIL_ON_VMREST_ERROR(dwError);
2654
- 
2655
-     fclose(fp);
2656
-+    fp = NULL;
2657
- 
2658
-     dwError = VmRESTSetSuccessResponse(
2659
-                   pRequest,
2660
-@@ -368,8 +391,20 @@
2661
-     BAIL_ON_VMREST_ERROR(dwError);
2662
- 
2663
- cleanup:
2664
-+    if (AllData != NULL)
2665
-+    {
2666
-+        free(AllData);
2667
-+        AllData = NULL;
2668
-+    }
2669
-+
2670
-     return dwError;
2671
- error:
2672
-+
2673
-+    if (fp != NULL)
2674
-+    {
2675
-+        fclose(fp);
2676
-+        fp = NULL;
2677
-+    }
2678
-     goto cleanup;
2679
- }
2680
- 
2681
-diff -ru c-rest-engine-1.0.3/transport/posix/global.c c-rest-engine-1.0.4/transport/posix/global.c
2682
-+++ c-rest-engine-1.0.4/transport/posix/global.c	2017-08-21 16:40:55.000000000 -0700
2683
-@@ -15,6 +15,6 @@
2684
- 
2685
- int                                  gSSLisedInstaceCount = INVALID;
2686
- pthread_mutex_t*                     gSSLThreadLock = NULL;
2687
--pthread_mutex_t                      gGlobalMutex;
2688
-+pthread_mutex_t                      gGlobalMutex = PTHREAD_MUTEX_INITIALIZER;
2689
- 
2690
- 
2691
-diff -ru c-rest-engine-1.0.3/transport/posix/socket.c c-rest-engine-1.0.4/transport/posix/socket.c
2692
-+++ c-rest-engine-1.0.4/transport/posix/socket.c	2017-08-21 16:40:55.000000000 -0700
2693
-@@ -266,15 +266,6 @@
2694
-     )
2695
- {
2696
-     DWORD                            dwError = REST_ENGINE_SUCCESS;
2697
--
2698
--    if (!pRESTHandle)
2699
--    {
2700
--        VMREST_LOG_DEBUG(pRESTHandle,"Invalid REST Handler");
2701
--        dwError = REST_ERROR_INVALID_HANDLER;
2702
--    }
2703
--    BAIL_ON_VMREST_ERROR(dwError);
2704
--
2705
--
2706
-     union
2707
-     {
2708
- #ifdef AF_INET6
2709
-@@ -294,6 +285,12 @@
2710
-     PVM_SOCKET                       pSocket = NULL;
2711
-     PVM_SOCK_SSL_INFO                pSSLInfo = NULL;
2712
- 
2713
-+    if (!pRESTHandle)
2714
-+    {
2715
-+        dwError = REST_ERROR_INVALID_HANDLER;
2716
-+    }
2717
-+    BAIL_ON_VMREST_ERROR(dwError);
2718
-+
2719
-     if (dwFlags & VM_SOCK_CREATE_FLAGS_IPV6)
2720
-     {
2721
- #ifdef AF_INET6
2722
-@@ -324,11 +321,6 @@
2723
-     /**** Check if connection is over SSL ****/
2724
-     if(dwFlags & VM_SOCK_IS_SSL)
2725
-     {
2726
--        if (gSSLisedInstaceCount == INVALID)
2727
--        {
2728
--            pthread_mutex_init(&gGlobalMutex, NULL);
2729
--            gSSLisedInstaceCount = 0;
2730
--        }
2731
-         SSL_library_init();
2732
-         dwError = VmRESTSecureSocket(
2733
-                       pRESTHandle,
2734
-@@ -337,15 +329,16 @@
2735
-                       );
2736
-         BAIL_ON_POSIX_SOCK_ERROR(dwError);
2737
-         pSSLInfo->isSecure = 1;
2738
-+        
2739
-         pthread_mutex_lock(&gGlobalMutex);
2740
--        if (gSSLisedInstaceCount == 0)
2741
-+        if (gSSLisedInstaceCount == INVALID)
2742
-         {
2743
-+            gSSLisedInstaceCount = 0;
2744
-             dwError = VmRESTSSLThreadLockInit();
2745
--            gSSLisedInstaceCount++;
2746
-         }
2747
-+        gSSLisedInstaceCount++;
2748
-         pthread_mutex_unlock(&gGlobalMutex);
2749
-         BAIL_ON_VMREST_ERROR(dwError);
2750
--        
2751
-     }
2752
-     else
2753
-     {
2754
-@@ -727,34 +720,33 @@
2755
-                              ssl = SSL_new(pRESTHandle->pSSLInfo->sslContext);
2756
-                              SSL_set_fd(ssl,pSocket->fd);
2757
- retry:
2758
--                             if ((SSL_accept(ssl) == -1) && (timeOutSec >= 0))
2759
-+                             if ( SSL_accept(ssl) == -1 )
2760
-                              {
2761
-                                  if (timeOutSec >= 0)
2762
-                                  {
2763
--                                 cntRty++;
2764
-+                                     cntRty++;
2765
- #ifdef WIN32
2766
--                                 Sleep(timerMs);
2767
-+                                     Sleep(timerMs);
2768
- #else
2769
--                                 usleep((timerMs * 1000));
2770
-+                                     usleep((timerMs * 1000));
2771
- #endif
2772
--                                 if (cntRty >= maxTry)
2773
--                                 {
2774
--                                     timerMs = ((timerMs >= 1000) ? 1000 : (timerMs*10));
2775
--                                     maxTry = ((maxTry <= 1) ? 1 : (maxTry/10));
2776
--                                     timeOutSec--;
2777
--                                     cntRty = 0;
2778
--                                 }
2779
--                                 goto retry;
2780
-+                                     if (cntRty >= maxTry)
2781
-+                                     {
2782
-+                                         timerMs = ((timerMs >= 1000) ? 1000 : (timerMs*10));
2783
-+                                         maxTry = ((maxTry <= 1) ? 1 : (maxTry/10));
2784
-+                                         timeOutSec--;
2785
-+                                         cntRty = 0;
2786
-+                                     }
2787
-+                                     goto retry;
2788
-                                  }
2789
-- 
2790
--                                 else if(timeOutSec <= 0)
2791
-+                                 else
2792
-                                  {
2793
--                                 VMREST_LOG_ERROR(pRESTHandle,"SSL accept failed");
2794
--                                  SSL_shutdown(ssl);
2795
--                                  SSL_free(ssl);
2796
--                                  close(pSocket->fd);
2797
--                                  dwError = VMREST_TRANSPORT_SSL_ACCEPT_FAILED;
2798
--                                  BAIL_ON_VMREST_ERROR(dwError);
2799
-+                                     VMREST_LOG_ERROR(pRESTHandle,"SSL accept failed");
2800
-+                                     SSL_shutdown(ssl);
2801
-+                                     SSL_free(ssl);
2802
-+                                     close(pSocket->fd);
2803
-+                                     dwError = VMREST_TRANSPORT_SSL_ACCEPT_FAILED;
2804
-+                                     BAIL_ON_VMREST_ERROR(dwError);
2805
-                                  }
2806
-                              }
2807
-                              pSocket->ssl = ssl;