• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef HEADER_CURL_MIME_H
2 #define HEADER_CURL_MIME_H
3 /***************************************************************************
4  *                                  _   _ ____  _
5  *  Project                     ___| | | |  _ \| |
6  *                             / __| | | | |_) | |
7  *                            | (__| |_| |  _ <| |___
8  *                             \___|\___/|_| \_\_____|
9  *
10  * Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, 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 #include "curl_setup.h"
26 
27 #define MIME_RAND_BOUNDARY_CHARS        16  /* Nb. of random boundary chars. */
28 #define MAX_ENCODED_LINE_LENGTH         76  /* Maximum encoded line length. */
29 #define ENCODING_BUFFER_SIZE            256 /* Encoding temp buffers size. */
30 
31 /* Part flags. */
32 #define MIME_USERHEADERS_OWNER  (1 << 0)
33 #define MIME_BODY_ONLY          (1 << 1)
34 #define MIME_FAST_READ          (1 << 2)
35 
36 #define FILE_CONTENTTYPE_DEFAULT        "application/octet-stream"
37 #define MULTIPART_CONTENTTYPE_DEFAULT   "multipart/mixed"
38 #define DISPOSITION_DEFAULT             "attachment"
39 
40 /* Part source kinds. */
41 enum mimekind {
42   MIMEKIND_NONE = 0,            /* Part not set. */
43   MIMEKIND_DATA,                /* Allocated mime data. */
44   MIMEKIND_FILE,                /* Data from file. */
45   MIMEKIND_CALLBACK,            /* Data from `read' callback. */
46   MIMEKIND_MULTIPART,           /* Data is a mime subpart. */
47   MIMEKIND_LAST
48 };
49 
50 /* Readback state tokens. */
51 enum mimestate {
52   MIMESTATE_BEGIN,              /* Readback has not yet started. */
53   MIMESTATE_CURLHEADERS,        /* In curl-generated headers. */
54   MIMESTATE_USERHEADERS,        /* In caller's supplied headers. */
55   MIMESTATE_EOH,                /* End of headers. */
56   MIMESTATE_BODY,               /* Placeholder. */
57   MIMESTATE_BOUNDARY1,          /* In boundary prefix. */
58   MIMESTATE_BOUNDARY2,          /* In boundary. */
59   MIMESTATE_CONTENT,            /* In content. */
60   MIMESTATE_END,                /* End of part reached. */
61   MIMESTATE_LAST
62 };
63 
64 /* Mime headers strategies. */
65 enum mimestrategy {
66   MIMESTRATEGY_MAIL,            /* Mime mail. */
67   MIMESTRATEGY_FORM,            /* HTTP post form. */
68   MIMESTRATEGY_LAST
69 };
70 
71 /* Content transfer encoder. */
72 struct mime_encoder {
73   const char *   name;          /* Encoding name. */
74   size_t         (*encodefunc)(char *buffer, size_t size, bool ateof,
75                                curl_mimepart *part);  /* Encoded read. */
76   curl_off_t     (*sizefunc)(curl_mimepart *part);  /* Encoded size. */
77 };
78 
79 /* Content transfer encoder state. */
80 struct mime_encoder_state {
81   size_t         pos;           /* Position on output line. */
82   size_t         bufbeg;        /* Next data index in input buffer. */
83   size_t         bufend;        /* First unused byte index in input buffer. */
84   char           buf[ENCODING_BUFFER_SIZE]; /* Input buffer. */
85 };
86 
87 /* Mime readback state. */
88 struct mime_state {
89   enum mimestate state;       /* Current state token. */
90   void *ptr;                  /* State-dependent pointer. */
91   curl_off_t offset;          /* State-dependent offset. */
92 };
93 
94 /* minimum buffer size for the boundary string */
95 #define MIME_BOUNDARY_LEN (24 + MIME_RAND_BOUNDARY_CHARS + 1)
96 
97 /* A mime multipart. */
98 struct curl_mime {
99   struct Curl_easy *easy;          /* The associated easy handle. */
100   curl_mimepart *parent;           /* Parent part. */
101   curl_mimepart *firstpart;        /* First part. */
102   curl_mimepart *lastpart;         /* Last part. */
103   char boundary[MIME_BOUNDARY_LEN]; /* The part boundary. */
104   struct mime_state state;         /* Current readback state. */
105 };
106 
107 /* A mime part. */
108 struct curl_mimepart {
109   struct Curl_easy *easy;          /* The associated easy handle. */
110   curl_mime *parent;               /* Parent mime structure. */
111   curl_mimepart *nextpart;         /* Forward linked list. */
112   enum mimekind kind;              /* The part kind. */
113   char *data;                      /* Memory data or file name. */
114   curl_read_callback readfunc;     /* Read function. */
115   curl_seek_callback seekfunc;     /* Seek function. */
116   curl_free_callback freefunc;     /* Argument free function. */
117   void *arg;                       /* Argument to callback functions. */
118   FILE *fp;                        /* File pointer. */
119   struct curl_slist *curlheaders;  /* Part headers. */
120   struct curl_slist *userheaders;  /* Part headers. */
121   char *mimetype;                  /* Part mime type. */
122   char *filename;                  /* Remote file name. */
123   char *name;                      /* Data name. */
124   curl_off_t datasize;             /* Expected data size. */
125   unsigned int flags;              /* Flags. */
126   struct mime_state state;         /* Current readback state. */
127   const struct mime_encoder *encoder; /* Content data encoder. */
128   struct mime_encoder_state encstate; /* Data encoder state. */
129   size_t lastreadstatus;           /* Last read callback returned status. */
130 };
131 
132 CURLcode Curl_mime_add_header(struct curl_slist **slp, const char *fmt, ...);
133 
134 #if (!defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_MIME)) ||     \
135   !defined(CURL_DISABLE_SMTP) || !defined(CURL_DISABLE_IMAP)
136 
137 /* Prototypes. */
138 void Curl_mime_initpart(struct curl_mimepart *part, struct Curl_easy *easy);
139 void Curl_mime_cleanpart(struct curl_mimepart *part);
140 CURLcode Curl_mime_duppart(struct curl_mimepart *dst,
141                            const curl_mimepart *src);
142 CURLcode Curl_mime_set_subparts(struct curl_mimepart *part,
143                                 struct curl_mime *subparts,
144                                 int take_ownership);
145 CURLcode Curl_mime_prepare_headers(struct curl_mimepart *part,
146                                    const char *contenttype,
147                                    const char *disposition,
148                                    enum mimestrategy strategy);
149 curl_off_t Curl_mime_size(struct curl_mimepart *part);
150 size_t Curl_mime_read(char *buffer, size_t size, size_t nitems,
151                       void *instream);
152 CURLcode Curl_mime_rewind(struct curl_mimepart *part);
153 const char *Curl_mime_contenttype(const char *filename);
154 void Curl_mime_unpause(struct curl_mimepart *part);
155 
156 #else
157 /* if disabled */
158 #define Curl_mime_initpart(x,y)
159 #define Curl_mime_cleanpart(x)
160 #define Curl_mime_duppart(x,y) CURLE_OK /* Nothing to duplicate. Succeed */
161 #define Curl_mime_set_subparts(a,b,c) CURLE_NOT_BUILT_IN
162 #define Curl_mime_prepare_headers(a,b,c,d) CURLE_NOT_BUILT_IN
163 #define Curl_mime_size(x) (curl_off_t) -1
164 #define Curl_mime_read NULL
165 #define Curl_mime_rewind(x) ((void)x, CURLE_NOT_BUILT_IN)
166 #define Curl_mime_unpause(x)
167 #endif
168 
169 
170 #endif /* HEADER_CURL_MIME_H */
171