• 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 - 2019, 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 #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 /* minimum buffer size for the boundary string */
92 #define MIME_BOUNDARY_LEN (24 + MIME_RAND_BOUNDARY_CHARS + 1)
93 
94 /* A mime multipart. */
95 struct curl_mime_s {
96   struct Curl_easy *easy;          /* The associated easy handle. */
97   curl_mimepart *parent;           /* Parent part. */
98   curl_mimepart *firstpart;        /* First part. */
99   curl_mimepart *lastpart;         /* Last part. */
100   char boundary[MIME_BOUNDARY_LEN]; /* The part boundary. */
101   mime_state state;                /* Current readback state. */
102 };
103 
104 /* A mime part. */
105 struct curl_mimepart_s {
106   struct Curl_easy *easy;          /* The associated easy handle. */
107   curl_mime *parent;               /* Parent mime structure. */
108   curl_mimepart *nextpart;         /* Forward linked list. */
109   enum mimekind kind;              /* The part kind. */
110   char *data;                      /* Memory data or file name. */
111   curl_read_callback readfunc;     /* Read function. */
112   curl_seek_callback seekfunc;     /* Seek function. */
113   curl_free_callback freefunc;     /* Argument free function. */
114   void *arg;                       /* Argument to callback functions. */
115   FILE *fp;                        /* File pointer. */
116   struct curl_slist *curlheaders;  /* Part headers. */
117   struct curl_slist *userheaders;  /* Part headers. */
118   char *mimetype;                  /* Part mime type. */
119   char *filename;                  /* Remote file name. */
120   char *name;                      /* Data name. */
121   curl_off_t datasize;             /* Expected data size. */
122   unsigned int flags;              /* Flags. */
123   mime_state state;                /* Current readback state. */
124   const mime_encoder *encoder;     /* Content data encoder. */
125   mime_encoder_state encstate;     /* Data encoder state. */
126 };
127 
128 
129 /* Prototypes. */
130 void Curl_mime_initpart(curl_mimepart *part, struct Curl_easy *easy);
131 void Curl_mime_cleanpart(curl_mimepart *part);
132 CURLcode Curl_mime_duppart(curl_mimepart *dst, const curl_mimepart *src);
133 CURLcode Curl_mime_set_subparts(curl_mimepart *part,
134                                 curl_mime *subparts, int take_ownership);
135 CURLcode Curl_mime_prepare_headers(curl_mimepart *part,
136                                    const char *contenttype,
137                                    const char *disposition,
138                                    enum mimestrategy strategy);
139 curl_off_t Curl_mime_size(curl_mimepart *part);
140 size_t Curl_mime_read(char *buffer, size_t size, size_t nitems,
141                       void *instream);
142 CURLcode Curl_mime_rewind(curl_mimepart *part);
143 CURLcode Curl_mime_add_header(struct curl_slist **slp, const char *fmt, ...);
144 const char *Curl_mime_contenttype(const char *filename);
145 
146 #endif /* HEADER_CURL_MIME_H */
147