2017-09-03 00:47:10 +08:00
|
|
|
#ifndef HEADER_CURL_MIME_H
|
|
|
|
#define HEADER_CURL_MIME_H
|
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2022-02-10 20:37:48 +08:00
|
|
|
* Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2017-09-03 00:47:10 +08:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2020-11-04 21:02:01 +08:00
|
|
|
* are also available at https://curl.se/docs/copyright.html.
|
2017-09-03 00:47:10 +08:00
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
2020-03-07 18:51:00 +08:00
|
|
|
* SPDX-License-Identifier: curl
|
2022-05-17 17:16:50 +08:00
|
|
|
*
|
2017-09-03 00:47:10 +08:00
|
|
|
***************************************************************************/
|
|
|
|
|
2019-05-05 23:08:21 +08:00
|
|
|
#include "curl_setup.h"
|
|
|
|
|
2022-02-12 06:21:38 +08:00
|
|
|
#define MIME_BOUNDARY_DASHES 24 /* leading boundary dashes */
|
2017-09-03 00:47:10 +08:00
|
|
|
#define MIME_RAND_BOUNDARY_CHARS 16 /* Nb. of random boundary chars. */
|
2017-09-06 00:11:59 +08:00
|
|
|
#define MAX_ENCODED_LINE_LENGTH 76 /* Maximum encoded line length. */
|
|
|
|
#define ENCODING_BUFFER_SIZE 256 /* Encoding temp buffers size. */
|
2017-09-03 00:47:10 +08:00
|
|
|
|
|
|
|
/* Part flags. */
|
|
|
|
#define MIME_USERHEADERS_OWNER (1 << 0)
|
|
|
|
#define MIME_BODY_ONLY (1 << 1)
|
2020-03-07 18:51:00 +08:00
|
|
|
#define MIME_FAST_READ (1 << 2)
|
2017-09-03 00:47:10 +08:00
|
|
|
|
2018-02-04 16:23:11 +08:00
|
|
|
#define FILE_CONTENTTYPE_DEFAULT "application/octet-stream"
|
|
|
|
#define MULTIPART_CONTENTTYPE_DEFAULT "multipart/mixed"
|
|
|
|
#define DISPOSITION_DEFAULT "attachment"
|
|
|
|
|
2017-09-03 00:47:10 +08:00
|
|
|
/* Part source kinds. */
|
|
|
|
enum mimekind {
|
|
|
|
MIMEKIND_NONE = 0, /* Part not set. */
|
|
|
|
MIMEKIND_DATA, /* Allocated mime data. */
|
2017-09-06 20:42:03 +08:00
|
|
|
MIMEKIND_FILE, /* Data from file. */
|
2017-09-03 00:47:10 +08:00
|
|
|
MIMEKIND_CALLBACK, /* Data from `read' callback. */
|
|
|
|
MIMEKIND_MULTIPART, /* Data is a mime subpart. */
|
|
|
|
MIMEKIND_LAST
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Readback state tokens. */
|
|
|
|
enum mimestate {
|
|
|
|
MIMESTATE_BEGIN, /* Readback has not yet started. */
|
|
|
|
MIMESTATE_CURLHEADERS, /* In curl-generated headers. */
|
|
|
|
MIMESTATE_USERHEADERS, /* In caller's supplied headers. */
|
|
|
|
MIMESTATE_EOH, /* End of headers. */
|
|
|
|
MIMESTATE_BODY, /* Placeholder. */
|
|
|
|
MIMESTATE_BOUNDARY1, /* In boundary prefix. */
|
|
|
|
MIMESTATE_BOUNDARY2, /* In boundary. */
|
|
|
|
MIMESTATE_CONTENT, /* In content. */
|
|
|
|
MIMESTATE_END, /* End of part reached. */
|
|
|
|
MIMESTATE_LAST
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Mime headers strategies. */
|
|
|
|
enum mimestrategy {
|
|
|
|
MIMESTRATEGY_MAIL, /* Mime mail. */
|
|
|
|
MIMESTRATEGY_FORM, /* HTTP post form. */
|
|
|
|
MIMESTRATEGY_LAST
|
|
|
|
};
|
|
|
|
|
2017-09-06 00:11:59 +08:00
|
|
|
/* Content transfer encoder. */
|
2020-05-14 06:05:04 +08:00
|
|
|
struct mime_encoder {
|
2017-09-06 00:11:59 +08:00
|
|
|
const char * name; /* Encoding name. */
|
|
|
|
size_t (*encodefunc)(char *buffer, size_t size, bool ateof,
|
2019-05-05 23:08:21 +08:00
|
|
|
curl_mimepart *part); /* Encoded read. */
|
2017-09-06 00:11:59 +08:00
|
|
|
curl_off_t (*sizefunc)(curl_mimepart *part); /* Encoded size. */
|
2020-05-14 06:05:04 +08:00
|
|
|
};
|
2017-09-06 00:11:59 +08:00
|
|
|
|
|
|
|
/* Content transfer encoder state. */
|
2020-05-14 06:05:04 +08:00
|
|
|
struct mime_encoder_state {
|
2017-09-06 00:11:59 +08:00
|
|
|
size_t pos; /* Position on output line. */
|
|
|
|
size_t bufbeg; /* Next data index in input buffer. */
|
|
|
|
size_t bufend; /* First unused byte index in input buffer. */
|
|
|
|
char buf[ENCODING_BUFFER_SIZE]; /* Input buffer. */
|
2020-05-14 06:05:04 +08:00
|
|
|
};
|
2017-09-06 00:11:59 +08:00
|
|
|
|
2017-09-03 00:47:10 +08:00
|
|
|
/* Mime readback state. */
|
2020-05-14 06:05:04 +08:00
|
|
|
struct mime_state {
|
2017-09-03 00:47:10 +08:00
|
|
|
enum mimestate state; /* Current state token. */
|
2017-09-05 17:45:21 +08:00
|
|
|
void *ptr; /* State-dependent pointer. */
|
2020-03-07 18:51:00 +08:00
|
|
|
curl_off_t offset; /* State-dependent offset. */
|
2020-05-14 06:05:04 +08:00
|
|
|
};
|
2017-09-03 00:47:10 +08:00
|
|
|
|
2022-02-10 20:37:48 +08:00
|
|
|
/* Boundary string length. */
|
2022-02-12 06:21:38 +08:00
|
|
|
#define MIME_BOUNDARY_LEN (MIME_BOUNDARY_DASHES + MIME_RAND_BOUNDARY_CHARS)
|
2019-02-12 21:30:25 +08:00
|
|
|
|
2017-09-06 00:11:59 +08:00
|
|
|
/* A mime multipart. */
|
2020-05-14 06:05:04 +08:00
|
|
|
struct curl_mime {
|
2017-09-05 17:45:21 +08:00
|
|
|
curl_mimepart *parent; /* Parent part. */
|
|
|
|
curl_mimepart *firstpart; /* First part. */
|
|
|
|
curl_mimepart *lastpart; /* Last part. */
|
2022-02-10 20:37:48 +08:00
|
|
|
char boundary[MIME_BOUNDARY_LEN + 1]; /* The part boundary. */
|
2020-05-14 06:05:04 +08:00
|
|
|
struct mime_state state; /* Current readback state. */
|
2017-09-03 00:47:10 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/* A mime part. */
|
2020-05-14 06:05:04 +08:00
|
|
|
struct curl_mimepart {
|
2017-09-05 17:45:21 +08:00
|
|
|
curl_mime *parent; /* Parent mime structure. */
|
|
|
|
curl_mimepart *nextpart; /* Forward linked list. */
|
|
|
|
enum mimekind kind; /* The part kind. */
|
2021-01-17 05:17:51 +08:00
|
|
|
unsigned int flags; /* Flags. */
|
2017-09-05 17:45:21 +08:00
|
|
|
char *data; /* Memory data or file name. */
|
|
|
|
curl_read_callback readfunc; /* Read function. */
|
|
|
|
curl_seek_callback seekfunc; /* Seek function. */
|
|
|
|
curl_free_callback freefunc; /* Argument free function. */
|
|
|
|
void *arg; /* Argument to callback functions. */
|
2017-09-06 20:42:03 +08:00
|
|
|
FILE *fp; /* File pointer. */
|
2017-09-05 17:45:21 +08:00
|
|
|
struct curl_slist *curlheaders; /* Part headers. */
|
|
|
|
struct curl_slist *userheaders; /* Part headers. */
|
|
|
|
char *mimetype; /* Part mime type. */
|
|
|
|
char *filename; /* Remote file name. */
|
|
|
|
char *name; /* Data name. */
|
|
|
|
curl_off_t datasize; /* Expected data size. */
|
2020-05-14 06:05:04 +08:00
|
|
|
struct mime_state state; /* Current readback state. */
|
|
|
|
const struct mime_encoder *encoder; /* Content data encoder. */
|
|
|
|
struct mime_encoder_state encstate; /* Data encoder state. */
|
2020-03-06 16:46:39 +08:00
|
|
|
size_t lastreadstatus; /* Last read callback returned status. */
|
2017-09-03 00:47:10 +08:00
|
|
|
};
|
|
|
|
|
2019-09-17 05:25:48 +08:00
|
|
|
CURLcode Curl_mime_add_header(struct curl_slist **slp, const char *fmt, ...);
|
|
|
|
|
2022-09-29 00:12:15 +08:00
|
|
|
#if !defined(CURL_DISABLE_MIME) && (!defined(CURL_DISABLE_HTTP) || \
|
|
|
|
!defined(CURL_DISABLE_SMTP) || \
|
|
|
|
!defined(CURL_DISABLE_IMAP))
|
2017-09-03 00:47:10 +08:00
|
|
|
|
|
|
|
/* Prototypes. */
|
2022-11-17 00:40:11 +08:00
|
|
|
void Curl_mime_initpart(struct curl_mimepart *part);
|
2020-05-14 06:05:04 +08:00
|
|
|
void Curl_mime_cleanpart(struct curl_mimepart *part);
|
2022-11-17 00:40:11 +08:00
|
|
|
CURLcode Curl_mime_duppart(struct Curl_easy *data,
|
|
|
|
struct curl_mimepart *dst,
|
2020-05-14 06:05:04 +08:00
|
|
|
const curl_mimepart *src);
|
|
|
|
CURLcode Curl_mime_set_subparts(struct curl_mimepart *part,
|
|
|
|
struct curl_mime *subparts,
|
|
|
|
int take_ownership);
|
2022-11-17 00:40:11 +08:00
|
|
|
CURLcode Curl_mime_prepare_headers(struct Curl_easy *data,
|
|
|
|
struct curl_mimepart *part,
|
2017-09-03 00:47:10 +08:00
|
|
|
const char *contenttype,
|
|
|
|
const char *disposition,
|
|
|
|
enum mimestrategy strategy);
|
2020-05-14 06:05:04 +08:00
|
|
|
curl_off_t Curl_mime_size(struct curl_mimepart *part);
|
2017-09-03 00:47:10 +08:00
|
|
|
size_t Curl_mime_read(char *buffer, size_t size, size_t nitems,
|
|
|
|
void *instream);
|
2020-05-14 06:05:04 +08:00
|
|
|
CURLcode Curl_mime_rewind(struct curl_mimepart *part);
|
2018-02-04 16:23:11 +08:00
|
|
|
const char *Curl_mime_contenttype(const char *filename);
|
2020-05-14 06:05:04 +08:00
|
|
|
void Curl_mime_unpause(struct curl_mimepart *part);
|
2017-09-03 00:47:10 +08:00
|
|
|
|
2019-05-05 23:08:21 +08:00
|
|
|
#else
|
|
|
|
/* if disabled */
|
2022-11-17 00:40:11 +08:00
|
|
|
#define Curl_mime_initpart(x)
|
2019-05-05 23:08:21 +08:00
|
|
|
#define Curl_mime_cleanpart(x)
|
2022-11-17 00:40:11 +08:00
|
|
|
#define Curl_mime_duppart(x,y,z) CURLE_OK /* Nothing to duplicate. Succeed */
|
2019-05-05 23:08:21 +08:00
|
|
|
#define Curl_mime_set_subparts(a,b,c) CURLE_NOT_BUILT_IN
|
2022-11-17 00:40:11 +08:00
|
|
|
#define Curl_mime_prepare_headers(a,b,c,d,e) CURLE_NOT_BUILT_IN
|
2019-05-05 23:08:21 +08:00
|
|
|
#define Curl_mime_size(x) (curl_off_t) -1
|
|
|
|
#define Curl_mime_read NULL
|
|
|
|
#define Curl_mime_rewind(x) ((void)x, CURLE_NOT_BUILT_IN)
|
2020-03-06 16:46:39 +08:00
|
|
|
#define Curl_mime_unpause(x)
|
2019-05-05 23:08:21 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2017-09-03 00:47:10 +08:00
|
|
|
#endif /* HEADER_CURL_MIME_H */
|