/*
* Copyright (c) 2003-2015 Hypertriton, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
* USE OF THIS SOFTWARE EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Implementation of our MIME Entity structure.
*/
#include "cgi.h"
#include
#include
#include
void
MIME_EntityInit(MIME_Entity *ment)
{
ment->headers = NULL;
ment->nheaders = 0;
ment->body = NULL;
ment->body_len = 0;
TAILQ_INIT(&ment->multiparts);
}
void
MIME_EntityDestroy(MIME_Entity *ment)
{
int i;
if (ment->nheaders > 0) {
for (i = 0; i < ment->nheaders; i++) {
free(ment->headers[i]);
}
free(ment->headers);
}
Free(ment->body);
}
/* Insert a new MIME header from a format string. */
void
MIME_AddHeader(MIME_Entity *ment, const char *fmt, ...)
{
va_list args;
char *buf;
va_start(args, fmt);
if (vasprintf(&buf, fmt, args) == -1) {
CGI_OutOfMem();
}
va_end(args);
if (ment->headers == NULL) {
ment->headers = Malloc(sizeof(char *));
} else {
ment->headers = Realloc(ment->headers,
(ment->nheaders+1) * sizeof(char *));
}
ment->headers[ment->nheaders++] = buf;
}
/* Insert a new MIME header from a string. */
void
MIME_AddHeaderS(MIME_Entity *ment, const char *s)
{
if (ment->headers == NULL) {
ment->headers = Malloc(sizeof(char *));
} else {
ment->headers = Realloc(ment->headers,
(ment->nheaders+1) * sizeof(char *));
}
ment->headers[ment->nheaders++] = Strdup(s);
}
/* Write MIME headers to query output. */
void
MIME_Write(CGI_Query *q, MIME_Entity *ment)
{
int i;
for (i = 0; i < ment->nheaders; i++) {
CGI_PutS(q, ment->headers[i]);
CGI_PutS(q, "\r\n");
}
CGI_PutS(q, "\r\n");
if (ment->body != NULL)
CGI_Write(q, ment->body, ment->body_len);
}