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