@@ -98,7 +98,7 @@ int WiFiPropConnection::identify(int *pVersion)
98
98
99
99
int WiFiPropConnection::loadImage (const uint8_t *image, int imageSize, uint8_t *response, int responseSize)
100
100
{
101
- uint8_t buffer[1024 ], *packet, *p ;
101
+ uint8_t buffer[1024 ], *packet, *body ;
102
102
int hdrCnt, result, cnt;
103
103
104
104
/* use the initial loader baud rate */
@@ -126,22 +126,13 @@ Content-Length: %d\r\n\
126
126
}
127
127
128
128
/* find the response body */
129
- p = buffer;
130
- while (cnt >= 4 && (p[0 ] != ' \r ' || p[1 ] != ' \n ' || p[2 ] != ' \r ' || p[3 ] != ' \n ' )) {
131
- --cnt;
132
- ++p;
133
- }
134
-
135
- /* make sure we found the \r\n\r\n that terminates the header */
136
- if (cnt < 4 )
129
+ if (!(body = getBody (buffer, cnt, &cnt)))
137
130
return -1 ;
138
- cnt -= 4 ;
139
- p += 4 ;
140
-
131
+
141
132
/* copy the body to the response if it fits */
142
- if (cnt > responseSize)
133
+ if (cnt != responseSize)
143
134
return -1 ;
144
- memcpy (response, p , cnt);
135
+ memcpy (response, body , cnt);
145
136
146
137
return 0 ;
147
138
}
@@ -351,15 +342,15 @@ bool WiFiPropConnection::isOpen()
351
342
352
343
int WiFiPropConnection::getVersion ()
353
344
{
354
- uint8_t buffer[1024 ];
355
- int hdrCnt, result, srcLen ;
356
- char *src, * dst;
345
+ uint8_t buffer[1024 ], *body ;
346
+ int hdrCnt, result, cnt ;
347
+ char *dst;
357
348
358
349
hdrCnt = snprintf ((char *)buffer, sizeof (buffer), " \
359
350
GET /wx/setting?name=version HTTP/1.1\r\n \
360
351
\r\n " );
361
352
362
- if (sendRequest (buffer, hdrCnt, buffer, sizeof (buffer), &result) == -1 ) {
353
+ if ((cnt = sendRequest (buffer, hdrCnt, buffer, sizeof (buffer), &result) ) == -1 ) {
363
354
message (" Get version failed" );
364
355
return -1 ;
365
356
}
@@ -368,28 +359,23 @@ GET /wx/setting?name=version HTTP/1.1\r\n\
368
359
return -1 ;
369
360
}
370
361
371
- src = (char *)buffer;
372
- srcLen = strlen (src);
373
-
374
- while (srcLen >= 4 ) {
375
- if (src[0 ] == ' \r ' && src[1 ] == ' \n ' && src[2 ] == ' \r ' && src[3 ] == ' \n ' )
376
- break ;
377
- --srcLen;
378
- ++src;
379
- }
380
- if (srcLen <= 4 ) {
362
+ if (!(body = getBody (buffer, cnt, &cnt)))
363
+ return -1 ;
364
+
365
+ if (cnt <= 0 ) {
381
366
message (" No version string" );
382
367
return -1 ;
383
368
}
384
369
385
- if (!(dst = (char *)malloc (srcLen - 4 + 1 ))) {
370
+ if (!(dst = (char *)malloc (cnt + 1 ))) {
386
371
nmessage (ERROR_INSUFFICIENT_MEMORY);
387
372
return -1 ;
388
373
}
389
374
390
375
if (m_version)
391
376
free (m_version);
392
- strcpy (dst, src + 4 );
377
+ strncpy (dst, (char *)body, cnt);
378
+ body[cnt] = ' \0 ' ;
393
379
m_version = dst;
394
380
395
381
return 0 ;
@@ -559,6 +545,26 @@ int WiFiPropConnection::sendRequest(uint8_t *req, int reqSize, uint8_t *res, int
559
545
return cnt;
560
546
}
561
547
548
+ uint8_t *WiFiPropConnection::getBody (uint8_t *msg, int msgSize, int *pBodySize)
549
+ {
550
+ uint8_t *p = msg;
551
+ int cnt = msgSize;
552
+
553
+ /* find the message body */
554
+ while (cnt >= 4 && (p[0 ] != ' \r ' || p[1 ] != ' \n ' || p[2 ] != ' \r ' || p[3 ] != ' \n ' )) {
555
+ --cnt;
556
+ ++p;
557
+ }
558
+
559
+ /* make sure we found the \r\n\r\n that terminates the header */
560
+ if (cnt < 4 )
561
+ return NULL ;
562
+
563
+ /* return the body */
564
+ *pBodySize = cnt - 4 ;
565
+ return p + 4 ;
566
+ }
567
+
562
568
void WiFiPropConnection::dumpHdr (const uint8_t *buf, int size)
563
569
{
564
570
int startOfLine = true ;
0 commit comments