Permalink
Browse files

Add some error handling.

  • Loading branch information...
1 parent 8a4f0fb commit a0f6b3b624219658b9b734a403167eca0c9ccb5c @FooBarWidget committed Jun 21, 2010
Showing with 19 additions and 1 deletion.
  1. +19 −1 multipart.cpp
View
@@ -84,6 +84,10 @@ class MultipartParser {
return false;
}
+ void setError(const char *message) {
+ state = ERROR;
+ }
+
public:
Callback onPartBegin;
Callback onHeaderField;
@@ -195,6 +199,7 @@ class MultipartParser {
cl = lower(c);
if (cl < A || cl > Z) {
+ setError("Malformed header name.");
return i;
}
break;
@@ -213,6 +218,7 @@ class MultipartParser {
break;
case HEADER_VALUE_ALMOST_DONE:
if (c != LF) {
+ setError("Malformed header value: LF expected after CR");
return i;
}
@@ -320,6 +326,18 @@ class MultipartParser {
return len;
}
+
+ bool succeeded() const {
+ return state == END;
+ }
+
+ bool hasError() const {
+ return state == ERROR;
+ }
+
+ bool stopped() const {
+ return state == ERROR || state == END;
+ }
};
#include <stdio.h>
@@ -373,7 +391,7 @@ main() {
size_t ret = parser.feed(buf + fed, len - fed);
fed += ret;
printf("accepted %d bytes\n", (int) ret);
- } while (fed < len);
+ } while (fed < len && !parser.stopped());
}
return 0;
}

0 comments on commit a0f6b3b

Please sign in to comment.