1 | #ifndef HEADER_CURL_MIME_H
|
---|
2 | #define HEADER_CURL_MIME_H
|
---|
3 | /***************************************************************************
|
---|
4 | * _ _ ____ _
|
---|
5 | * Project ___| | | | _ \| |
|
---|
6 | * / __| | | | |_) | |
|
---|
7 | * | (__| |_| | _ <| |___
|
---|
8 | * \___|\___/|_| \_\_____|
|
---|
9 | *
|
---|
10 | * Copyright (C) 1998 - 2018, Daniel Stenberg, <[email protected]>, et al.
|
---|
11 | *
|
---|
12 | * This software is licensed as described in the file COPYING, which
|
---|
13 | * you should have received as part of this distribution. The terms
|
---|
14 | * are also available at https://curl.haxx.se/docs/copyright.html.
|
---|
15 | *
|
---|
16 | * You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
---|
17 | * copies of the Software, and permit persons to whom the Software is
|
---|
18 | * furnished to do so, under the terms of the COPYING file.
|
---|
19 | *
|
---|
20 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
---|
21 | * KIND, either express or implied.
|
---|
22 | *
|
---|
23 | ***************************************************************************/
|
---|
24 |
|
---|
25 | #define MIME_RAND_BOUNDARY_CHARS 16 /* Nb. of random boundary chars. */
|
---|
26 | #define MAX_ENCODED_LINE_LENGTH 76 /* Maximum encoded line length. */
|
---|
27 | #define ENCODING_BUFFER_SIZE 256 /* Encoding temp buffers size. */
|
---|
28 |
|
---|
29 | /* Part flags. */
|
---|
30 | #define MIME_USERHEADERS_OWNER (1 << 0)
|
---|
31 | #define MIME_BODY_ONLY (1 << 1)
|
---|
32 |
|
---|
33 | #define FILE_CONTENTTYPE_DEFAULT "application/octet-stream"
|
---|
34 | #define MULTIPART_CONTENTTYPE_DEFAULT "multipart/mixed"
|
---|
35 | #define DISPOSITION_DEFAULT "attachment"
|
---|
36 |
|
---|
37 | /* Part source kinds. */
|
---|
38 | enum mimekind {
|
---|
39 | MIMEKIND_NONE = 0, /* Part not set. */
|
---|
40 | MIMEKIND_DATA, /* Allocated mime data. */
|
---|
41 | MIMEKIND_FILE, /* Data from file. */
|
---|
42 | MIMEKIND_CALLBACK, /* Data from `read' callback. */
|
---|
43 | MIMEKIND_MULTIPART, /* Data is a mime subpart. */
|
---|
44 | MIMEKIND_LAST
|
---|
45 | };
|
---|
46 |
|
---|
47 | /* Readback state tokens. */
|
---|
48 | enum mimestate {
|
---|
49 | MIMESTATE_BEGIN, /* Readback has not yet started. */
|
---|
50 | MIMESTATE_CURLHEADERS, /* In curl-generated headers. */
|
---|
51 | MIMESTATE_USERHEADERS, /* In caller's supplied headers. */
|
---|
52 | MIMESTATE_EOH, /* End of headers. */
|
---|
53 | MIMESTATE_BODY, /* Placeholder. */
|
---|
54 | MIMESTATE_BOUNDARY1, /* In boundary prefix. */
|
---|
55 | MIMESTATE_BOUNDARY2, /* In boundary. */
|
---|
56 | MIMESTATE_CONTENT, /* In content. */
|
---|
57 | MIMESTATE_END, /* End of part reached. */
|
---|
58 | MIMESTATE_LAST
|
---|
59 | };
|
---|
60 |
|
---|
61 | /* Mime headers strategies. */
|
---|
62 | enum mimestrategy {
|
---|
63 | MIMESTRATEGY_MAIL, /* Mime mail. */
|
---|
64 | MIMESTRATEGY_FORM, /* HTTP post form. */
|
---|
65 | MIMESTRATEGY_LAST
|
---|
66 | };
|
---|
67 |
|
---|
68 | /* Content transfer encoder. */
|
---|
69 | typedef struct {
|
---|
70 | const char * name; /* Encoding name. */
|
---|
71 | size_t (*encodefunc)(char *buffer, size_t size, bool ateof,
|
---|
72 | curl_mimepart *part); /* Encoded read. */
|
---|
73 | curl_off_t (*sizefunc)(curl_mimepart *part); /* Encoded size. */
|
---|
74 | } mime_encoder;
|
---|
75 |
|
---|
76 | /* Content transfer encoder state. */
|
---|
77 | typedef struct {
|
---|
78 | size_t pos; /* Position on output line. */
|
---|
79 | size_t bufbeg; /* Next data index in input buffer. */
|
---|
80 | size_t bufend; /* First unused byte index in input buffer. */
|
---|
81 | char buf[ENCODING_BUFFER_SIZE]; /* Input buffer. */
|
---|
82 | } mime_encoder_state;
|
---|
83 |
|
---|
84 | /* Mime readback state. */
|
---|
85 | typedef struct {
|
---|
86 | enum mimestate state; /* Current state token. */
|
---|
87 | void *ptr; /* State-dependent pointer. */
|
---|
88 | size_t offset; /* State-dependent offset. */
|
---|
89 | } mime_state;
|
---|
90 |
|
---|
91 | /* A mime multipart. */
|
---|
92 | struct curl_mime_s {
|
---|
93 | struct Curl_easy *easy; /* The associated easy handle. */
|
---|
94 | curl_mimepart *parent; /* Parent part. */
|
---|
95 | curl_mimepart *firstpart; /* First part. */
|
---|
96 | curl_mimepart *lastpart; /* Last part. */
|
---|
97 | char *boundary; /* The part boundary. */
|
---|
98 | mime_state state; /* Current readback state. */
|
---|
99 | };
|
---|
100 |
|
---|
101 | /* A mime part. */
|
---|
102 | struct curl_mimepart_s {
|
---|
103 | struct Curl_easy *easy; /* The associated easy handle. */
|
---|
104 | curl_mime *parent; /* Parent mime structure. */
|
---|
105 | curl_mimepart *nextpart; /* Forward linked list. */
|
---|
106 | enum mimekind kind; /* The part kind. */
|
---|
107 | char *data; /* Memory data or file name. */
|
---|
108 | curl_read_callback readfunc; /* Read function. */
|
---|
109 | curl_seek_callback seekfunc; /* Seek function. */
|
---|
110 | curl_free_callback freefunc; /* Argument free function. */
|
---|
111 | void *arg; /* Argument to callback functions. */
|
---|
112 | FILE *fp; /* File pointer. */
|
---|
113 | struct curl_slist *curlheaders; /* Part headers. */
|
---|
114 | struct curl_slist *userheaders; /* Part headers. */
|
---|
115 | char *mimetype; /* Part mime type. */
|
---|
116 | char *filename; /* Remote file name. */
|
---|
117 | char *name; /* Data name. */
|
---|
118 | curl_off_t datasize; /* Expected data size. */
|
---|
119 | unsigned int flags; /* Flags. */
|
---|
120 | mime_state state; /* Current readback state. */
|
---|
121 | const mime_encoder *encoder; /* Content data encoder. */
|
---|
122 | mime_encoder_state encstate; /* Data encoder state. */
|
---|
123 | };
|
---|
124 |
|
---|
125 |
|
---|
126 | /* Prototypes. */
|
---|
127 | void Curl_mime_initpart(curl_mimepart *part, struct Curl_easy *easy);
|
---|
128 | void Curl_mime_cleanpart(curl_mimepart *part);
|
---|
129 | CURLcode Curl_mime_duppart(curl_mimepart *dst, const curl_mimepart *src);
|
---|
130 | CURLcode Curl_mime_set_subparts(curl_mimepart *part,
|
---|
131 | curl_mime *subparts, int take_ownership);
|
---|
132 | CURLcode Curl_mime_prepare_headers(curl_mimepart *part,
|
---|
133 | const char *contenttype,
|
---|
134 | const char *disposition,
|
---|
135 | enum mimestrategy strategy);
|
---|
136 | curl_off_t Curl_mime_size(curl_mimepart *part);
|
---|
137 | size_t Curl_mime_read(char *buffer, size_t size, size_t nitems,
|
---|
138 | void *instream);
|
---|
139 | CURLcode Curl_mime_rewind(curl_mimepart *part);
|
---|
140 | CURLcode Curl_mime_add_header(struct curl_slist **slp, const char *fmt, ...);
|
---|
141 | const char *Curl_mime_contenttype(const char *filename);
|
---|
142 |
|
---|
143 | #endif /* HEADER_CURL_MIME_H */
|
---|