@@ -125,6 +125,47 @@ int WSAGetLastError() {
125
125
126
126
void doProcessing (int sock , char * baseDir );
127
127
128
+ int sockSend (int sock , const void * buf , size_t len ) {
129
+ #ifdef _WIN32
130
+ return send (sock , buf , len , 0 );
131
+ #else
132
+ return write (sock , buf , len );
133
+ #endif
134
+ }
135
+
136
+ int sockSendAll (int sock , const void * buf , size_t len ) {
137
+ const char * p = buf ;
138
+ while (0 < len ) {
139
+ int n = sockSend (sock , p , len );
140
+ if (0 >= n ) {
141
+ return -1 ;
142
+ }
143
+ p += n ;
144
+ len -= n ;
145
+ }
146
+ return 0 ;
147
+ }
148
+
149
+ int sendHttpResponse (int sock , int httpStatus , char * payload , int size ) {
150
+ // send HTTP headers
151
+ char buf [200 ];
152
+ int n = sprintf (buf , "HTTP/1.1 %d OK\r\nContent-Length: %d\r\n\r\n" , httpStatus , size );
153
+ if (0 >= n ) {
154
+ perror ("sprintf failed" );
155
+ return -1 ;
156
+ }
157
+ if (0 > sockSendAll (sock , buf , n )) {
158
+ perror ("Failed to send HTTP response headers" );
159
+ return -1 ;
160
+ }
161
+
162
+ // send payload
163
+ if (0 > sockSendAll (sock , payload , size )) {
164
+ perror ("Failed to send HTTP response body" );
165
+ return -1 ;
166
+ }
167
+ }
168
+
128
169
void sendErrorMessage (int sock , char * errorMessage , const char * errorCode ) {
129
170
perror (errorMessage );
130
171
@@ -141,12 +182,7 @@ void sendErrorMessage(int sock, char * errorMessage, const char *errorCode) {
141
182
perror ("[ERROR]: resultJSONtext cannot be created" );
142
183
return ;
143
184
}
144
- #ifdef _WIN32
145
- int n = send ( sock , resultJSONtext , strlen (resultJSONtext ), 0 );
146
- #else
147
- int n = write (sock , resultJSONtext , strlen (resultJSONtext ));
148
- #endif
149
-
185
+ int n = sendHttpResponse (sock , 500 , resultJSONtext , strlen (resultJSONtext ));
150
186
if (n < 0 ) {
151
187
perror ("ERROR writing to socket" );
152
188
return ;
@@ -530,7 +566,41 @@ void doProcessingWin (struct thread_win_params* ptwp)
530
566
}
531
567
#endif
532
568
569
+ /**
570
+ * Tries to detect message length by the given buffer, which contains the beginning of the message.
571
+ * The buffer passed must be of at least (size+1) length.
572
+ *
573
+ * Returns -1, if the length is still unknown (in this case the caller must provide a bigger part of the message).
574
+ *
575
+ * Returns -2, if the length can never be determined, i.e. HTTP headers don't contain 'Content-Length'.
576
+ *
577
+ * If 0 or more is returned, it is the total size of the message (size of the headers + size of the body).
578
+ */
579
+ int detectMessageLength (char * buf , int size ) {
580
+ buf [size ] = 0 ;
581
+
582
+ // trying to find where headers end
583
+ char * s = strstr (buf , "\r\n\r\n" );
584
+ int header_stop_len = 4 ;
585
+ if (NULL == s ) {
586
+ s = strstr (buf , "\n\n" );
587
+ header_stop_len = 2 ;
588
+ }
589
+ if (NULL == s ) {
590
+ return -1 ;
591
+ }
592
+ const long header_len = (s - buf ) + header_stop_len ;
593
+
594
+ const char * content_len_key = "Content-Length:" ;
595
+ // trying to find Content-Length
596
+ char * content_len_header = strstr (buf , content_len_key );
597
+ if (NULL == content_len_header || (content_len_header - buf ) >= header_len ) {
598
+ return -2 ;
599
+ }
533
600
601
+ long l = strtol (content_len_header + strlen (content_len_key ), NULL , 10 );
602
+ return header_len + l ;
603
+ }
534
604
535
605
void doProcessing (int sock , char * baseDir ) {
536
606
char * client_message = malloc (MAX_BUFFER_SIZE + 1 );
@@ -548,6 +618,7 @@ void doProcessing(int sock, char *baseDir) {
548
618
memset (buffer , 0 , MAX_BUFFER_SIZE );
549
619
int buffer_read = 0 ;
550
620
int total_read = 0 ;
621
+ int message_len = -1 ;
551
622
552
623
//buffered read from socket
553
624
int i = 0 ;
@@ -564,12 +635,15 @@ void doProcessing(int sock, char *baseDir) {
564
635
total_read = total_read + buffer_read ;
565
636
printf ("Next %i part of buffer\n" , i );
566
637
i ++ ;
638
+ if (-1 == message_len ) {
639
+ message_len = detectMessageLength (buffer , total_read );
640
+ }
567
641
} else if (buffer_read < 0 ) {
568
642
perror ("[ERROR]: reading from socket" );
569
643
printf ("WSAGetLastError() %i\n" , WSAGetLastError ()); //win
570
644
exit (1 );
571
645
}
572
- if (buffer_read == 0 || buffer_read < MAX_BUFFER_SIZE ) {
646
+ if (buffer_read == 0 || total_read >= message_len || -2 == message_len ) {
573
647
/* message received successfully */
574
648
break ;
575
649
}
@@ -607,10 +681,8 @@ void doProcessing(int sock, char *baseDir) {
607
681
return ;
608
682
}
609
683
char * clientSecretKey = secretkeyJSON -> valuestring ;
610
- printf ("[ERROR ]: Get secretkey: %s from client\n" , clientSecretKey );
684
+ printf ("[DEBUG ]: Got secretkey: %s from client\n" , clientSecretKey );
611
685
if (!serverSecretKey || strncmp (clientSecretKey , serverSecretKey , strlen (serverSecretKey )) == 0 ) {
612
-
613
-
614
686
cJSON * actionJSON = cJSON_GetObjectItem (commandJSON , JSON_PARAM_NAME_COMMAND );
615
687
if (!actionJSON ) {
616
688
printf ("[ERROR]: Invalid action JSON format for message: \n" );
@@ -879,11 +951,7 @@ void doProcessing(int sock, char *baseDir) {
879
951
sendErrorMessage (sock , "unknown action" , ERROR_CODE );
880
952
}
881
953
882
- #ifdef _WIN32
883
- int n1 = send ( sock , resultJSONtext , strlen (resultJSONtext ), 0 );
884
- #else
885
- int n1 = write (sock , resultJSONtext , strlen (resultJSONtext ));
886
- #endif
954
+ int n1 = sendHttpResponse (sock , 200 , resultJSONtext , strlen (resultJSONtext ));
887
955
888
956
free (resultJSONtext );
889
957
0 commit comments