@@ -295,6 +295,25 @@ int wolfCLU_HttpServerRecv(SOCKET_T clientfd, byte* buffer, int bufferSz)
295295 return totalLen ;
296296}
297297
298+ /* Send all bytes, looping on partial writes and EINTR */
299+ int wolfCLU_SendAll (SOCKET_T sockfd , const char * buf , int len )
300+ {
301+ int sent = 0 ;
302+ while (sent < len ) {
303+ int n = (int )send (sockfd , buf + sent , (size_t )(len - sent ), 0 );
304+ if (n < 0 ) {
305+ #ifndef _WIN32
306+ if (errno == EINTR ) continue ;
307+ #endif
308+ return -1 ;
309+ }
310+ if (n == 0 )
311+ return -1 ;
312+ sent += n ;
313+ }
314+ return sent ;
315+ }
316+
298317/**
299318 * @brief Send an HTTP response with OCSP content
300319 * @param clientfd client socket descriptor
@@ -307,7 +326,6 @@ int wolfCLU_HttpServerSendOcspResponse(SOCKET_T clientfd, const byte* body,
307326{
308327 char header [512 ];
309328 int headerLen ;
310- int sent ;
311329
312330 headerLen = XSNPRINTF (header , sizeof (header ),
313331 "HTTP/1.0 200 OK\r\n"
@@ -321,15 +339,13 @@ int wolfCLU_HttpServerSendOcspResponse(SOCKET_T clientfd, const byte* body,
321339 }
322340
323341 /* Send header */
324- sent = (int )send (clientfd , header , (size_t )headerLen , 0 );
325- if (sent != headerLen ) {
342+ if (wolfCLU_SendAll (clientfd , header , headerLen ) != headerLen ) {
326343 return -1 ;
327344 }
328345
329346 /* Send body */
330347 if (bodySz > 0 ) {
331- sent = (int )send (clientfd , (const char * )body , (size_t )bodySz , 0 );
332- if (sent != bodySz ) {
348+ if (wolfCLU_SendAll (clientfd , (const char * )body , bodySz ) != bodySz ) {
333349 return -1 ;
334350 }
335351 }
@@ -390,12 +406,18 @@ int wolfCLU_HttpServerParseRequest(const byte* httpReq, int httpReqSz,
390406{
391407 const char * contentLen ;
392408 const char * bodyStart ;
409+ int bodyAvail ;
393410
394411 * body = NULL ;
395412 * bodySz = 0 ;
396413
414+ if (httpReqSz < (int )XSTR_SIZEOF ("POST " )) {
415+ return -1 ;
416+ }
417+
397418 /* Check for POST method */
398- if (XSTRNCMP ((char * )httpReq , "POST " , 5 ) != 0 ) {
419+ if (XSTRNCMP ((char * )httpReq , "POST " ,
420+ XSTR_SIZEOF ("POST " )) != 0 ) {
399421 return -1 ;
400422 }
401423
@@ -405,22 +427,26 @@ int wolfCLU_HttpServerParseRequest(const byte* httpReq, int httpReqSz,
405427 contentLen = XSTRSTR ((char * )httpReq , "content-length:" );
406428 }
407429 if (contentLen ) {
408- * bodySz = XATOI (contentLen + 15 );
409- if (* bodySz < 0 ) {
430+ * bodySz = XATOI (contentLen + XSTR_SIZEOF ( "Content-Length:" ) );
431+ if (* bodySz <= 0 ) {
410432 return -1 ;
411433 }
412434 }
413435
414- /* Find body (after \r\n\r\n) */
415- bodyStart = XSTRSTR ((char * )httpReq , "\r\n\r\n" );
416- if (bodyStart ) {
417- * body = (const byte * )(bodyStart + 4 );
418- /* Use Content-Length if available, otherwise use remaining data */
419- if (* bodySz == 0 ) {
420- * bodySz = httpReqSz - (int )(* body - httpReq );
421- }
422- return 0 ;
436+ /* Find body (has to appear after headers) */
437+ bodyStart = XSTRSTR ((char * )contentLen , "\r\n\r\n" );
438+ if (!bodyStart )
439+ return -1 ;
440+ bodyAvail = (int )(((char * )httpReq + httpReqSz ) -
441+ (bodyStart + XSTR_SIZEOF ("\r\n\r\n" )));
442+ /* Use Content-Length if available, otherwise use
443+ * remaining data. Verify how much body we have. */
444+ if (* bodySz == 0 ) {
445+ * bodySz = bodyAvail ;
423446 }
424-
425- return -1 ;
447+ else if (* bodySz > bodyAvail ) {
448+ return -1 ;
449+ }
450+ * body = (const byte * )(bodyStart + XSTR_SIZEOF ("\r\n\r\n" ));
451+ return 0 ;
426452}
0 commit comments