-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathmultipart_parser.h
115 lines (101 loc) · 3.18 KB
/
multipart_parser.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/* Based on node-formidable by Felix Geisendörfer
* Igor Afonov - [email protected] - 2012
* MIT License - http://www.opensource.org/licenses/mit-license.php
* @link https://github.com/libcat/libcat/blob/develop/deps/multipart_parser
*/
#ifndef _multipart_parser_h
#define _multipart_parser_h
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
typedef struct multipart_parser multipart_parser;
typedef struct multipart_parser_settings multipart_parser_settings;
typedef struct multipart_parser_state multipart_parser_state;
typedef int (*multipart_data_cb)(multipart_parser *, const char *at, size_t length);
typedef int (*multipart_notify_cb)(multipart_parser *);
enum multipart_error {
MPPE_OK = 0,
MPPE_PAUSED,
MPPE_UNKNOWN,
MPPE_BOUNDARY_END_NO_CRLF,
MPPE_BAD_START_BOUNDARY,
MPPE_INVALID_HEADER_FIELD_CHAR,
MPPE_INVALID_HEADER_VALUE_CHAR,
MPPE_BAD_PART_END,
MPPE_END_BOUNDARY_NO_DASH,
MPPE_HEADER_VALUE_INCOMPLETE,
};
#define MPPE_ERROR -1
// from RFC2046
#define BOUNDARY_MAX_LEN 70
struct multipart_parser {
/* private holder for callbacks */
const multipart_parser_settings *settings;
/* private internal index for matching boundary */
size_t index;
/* public error unexpected char index */
size_t error_i;
/* private boundary length + 2 ("--") */
unsigned char boundary_length;
FILE *fp;
void *data;
/* private FSM state */
unsigned char state;
/* public error reason */
unsigned char error_reason;
/* private boundary storage: "--" + boundary */
char boundary[(2 + BOUNDARY_MAX_LEN) * 2 + 9];
/* public error expected char */
char error_expected;
/* public error unexpected char */
char error_unexpected;
};
struct multipart_parser_settings {
/*
* data callback called on header field coming
* for example data is "Content-Type" with length 12
*/
multipart_data_cb on_header_field;
/*
* data callback called on header value coming
* for example data is "plain/text" with length 10
*/
multipart_data_cb on_header_value;
/*
* data callback called on body data coming,
* will be called repeatedly until data end
*/
multipart_data_cb on_part_data;
/*
* before "--" boundary
*/
multipart_notify_cb on_part_data_begin;
/*
* after all headers line "\r\n", before body
*/
multipart_notify_cb on_headers_complete;
/*
* after body, before next "--" boundary
*/
multipart_notify_cb on_part_data_end;
/*
* after last "--" boundary "--"
*/
multipart_notify_cb on_body_end;
};
multipart_parser *multipart_parser_init(const char *boundary,
size_t boundary_length,
const multipart_parser_settings *settings);
void multipart_parser_free(multipart_parser *p);
/**
* The multipart header must be complete, otherwise it will be parsed incorrectly
*/
ssize_t multipart_parser_execute(multipart_parser *p, const char *buf, size_t len);
int multipart_parser_error_msg(multipart_parser *p, char *buf, size_t len);
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif