| 289 | } |
| 290 | |
| 291 | int HttpClient::responseStatusCode() |
| 292 | { |
| 293 | if (iState < eRequestSent) |
| 294 | { |
| 295 | return HTTP_ERROR_API; |
| 296 | } |
| 297 | // The first line will be of the form Status-Line: |
| 298 | // HTTP-Version SP Status-Code SP Reason-Phrase CRLF |
| 299 | // Where HTTP-Version is of the form: |
| 300 | // HTTP-Version = "HTTP" "/" 1*DIGIT "." 1*DIGIT |
| 301 | |
| 302 | char c = '\0'; |
| 303 | do |
| 304 | { |
| 305 | // Make sure the status code is reset, and likewise the state. This |
| 306 | // lets us easily cope with 1xx informational responses by just |
| 307 | // ignoring them really, and reading the next line for a proper response |
| 308 | iStatusCode = 0; |
| 309 | iState = eRequestSent; |
| 310 | |
| 311 | unsigned long timeoutStart = millis(); |
| 312 | // Psuedo-regexp we're expecting before the status-code |
| 313 | const char* statusPrefix = "HTTP/*.* "; |
| 314 | const char* statusPtr = statusPrefix; |
| 315 | // Whilst we haven't timed out & haven't reached the end of the headers |
| 316 | while ((c != '\n') && |
| 317 | ( (millis() - timeoutStart) < iHttpResponseTimeout )) |
| 318 | { |
| 319 | if (available()) |
| 320 | { |
| 321 | c = read(); |
| 322 | if (c != -1) |
| 323 | { |
| 324 | switch(iState) |
| 325 | { |
| 326 | case eRequestSent: |
| 327 | // We haven't reached the status code yet |
| 328 | if ( (*statusPtr == '*') || (*statusPtr == c) ) |
| 329 | { |
| 330 | // This character matches, just move along |
| 331 | statusPtr++; |
| 332 | if (*statusPtr == '\0') |
| 333 | { |
| 334 | // We've reached the end of the prefix |
| 335 | iState = eReadingStatusCode; |
| 336 | } |
| 337 | } |
| 338 | else |
| 339 | { |
| 340 | return HTTP_ERROR_INVALID_RESPONSE; |
| 341 | } |
| 342 | break; |
| 343 | case eReadingStatusCode: |
| 344 | if (isdigit(c)) |
| 345 | { |
| 346 | // This assumes we won't get more than the 3 digits we |
| 347 | // want |
| 348 | iStatusCode = iStatusCode*10 + (c - '0'); |
nothing calls this directly
no outgoing calls
no test coverage detected