Skip to content

Commit

Permalink
Add ability to retrieve the error message.
Browse files Browse the repository at this point in the history
  • Loading branch information
FooBarWidget committed Jun 21, 2010
1 parent c1136ee commit a5fd57c
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions multipart.cpp
Expand Up @@ -46,6 +46,7 @@ class MultipartParser {
size_t headerFieldMark;
size_t headerValueMark;
size_t partDataMark;
const char *errorReason;

void callback(Callback cb, const char *buffer = NULL, size_t start = UNMARKED, size_t end = UNMARKED) {
if (start != UNMARKED && start == end) {
Expand Down Expand Up @@ -89,6 +90,7 @@ class MultipartParser {

void setError(const char *message) {
state = ERROR;
errorReason = message;
}

public:
Expand Down Expand Up @@ -124,6 +126,7 @@ class MultipartParser {
headerFieldMark = UNMARKED;
headerValueMark = UNMARKED;
partDataMark = UNMARKED;
errorReason = NULL;

onPartBegin = NULL;
onHeaderField = NULL;
Expand Down Expand Up @@ -354,6 +357,10 @@ class MultipartParser {
bool stopped() const {
return state == ERROR || state == END;
}

const char *getErrorMessage() const {
return errorReason;
}
};

#include <stdio.h>
Expand Down Expand Up @@ -399,8 +406,8 @@ main() {
parser.onPartEnd = onPartEnd;
parser.onEnd = onEnd;

while (!feof(stdin)) {
char buf[1024 * 16];
while (!parser.stopped() && !feof(stdin)) {
char buf[5];
size_t len = fread(buf, 1, sizeof(buf), stdin);
size_t fed = 0;
do {
Expand All @@ -409,5 +416,6 @@ main() {
printf("accepted %d bytes\n", (int) ret);
} while (fed < len && !parser.stopped());
}
printf("%s\n", parser.getErrorMessage());
return 0;
}

0 comments on commit a5fd57c

Please sign in to comment.