asterisk/main/minimime/mm_parse.c
Russell Bryant 0a9750ef9f Merged revisions 60603 via svnmerge from
https://origsvn.digium.com/svn/asterisk/branches/1.4

........
r60603 | russell | 2007-04-06 15:58:43 -0500 (Fri, 06 Apr 2007) | 13 lines

To be able to achieve the things that we would like to achieve with the
Asterisk GUI project, we need a fully functional HTTP interface with access
to the Asterisk manager interface.  One of the things that was intended to be
a part of this system, but was never actually implemented, was the ability for
the GUI to be able to upload files to Asterisk.  So, this commit adds this in
the most minimally invasive way that we could come up with.

A lot of work on minimime was done by Steve Murphy.  He fixed a lot of bugs in
the parser, and updated it to be thread-safe.  The ability to check
permissions of active manager sessions was added by Dwayne Hubbard.  Then,
hacking this all together and do doing the modifications necessary to the HTTP
interface was done by me.

........


git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@60604 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2007-04-06 21:16:38 +00:00

182 lines
5.4 KiB
C

/*
* $Id$
*
* MiniMIME - a library for handling MIME messages
*
* Copyright (C) 2003 Jann Fischer <rezine@mistrust.net>
* 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.
* 3. Neither the name of the author nor the names of the contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY JANN FISCHER 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 JANN FISCHER OR THE VOICES IN HIS HEAD
* 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.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include "mm_internal.h"
#include "mm_util.h"
#include "mimeparser.h"
#include "mimeparser.tab.h"
int yyparse (struct parser_state *pstate, void *yyscanner);
void PARSER_initialize(struct parser_state *, void *);
void PARSER_setbuffer(const char *);
void PARSER_setfp(FILE *,void *yyscanner);
/** @file mm_parse.c
*
* Functions to parse MIME messages
*/
/**
* Parses a NUL-terminated string into a MiniMIME context
*
* @param ctx A valid MiniMIME context object
* @param text The NUL-terminated string to parse
* @param parsemode The parsemode
* @param flags The flags to pass to the parser
* @returns 0 on success or -1 on failure
* @note Sets mm_errno if an error occurs
*
* This function parses a MIME message, stored in the memory region pointed to
* by text (must be NUL-terminated) according to the parseflags and stores the
* results in the MiniMIME context specified by ctx.
*
* The following modes can be used to specify how the message should be
* parsed:
*
* - MM_PARSE_STRICT: Do not tolerate MIME violations
* - MM_PARSE_LOOSE: Tolerate as much MIME violations as possible
*
* The context needs to be initialized before using mm_context_new() and may
* be freed using mm_context_free().
*/
int
mm_parse_mem(MM_CTX *ctx, const char *text, int parsemode, int flags)
{
void *yyscanner;
int res;
struct parser_state pstate;
typedef void *yyscan_t;
int mimeparser_yylex_init (yyscan_t* scanner);
int mimeparser_yylex_destroy (yyscan_t yyscanner );
pstate.ctx = ctx;
pstate.parsemode = parsemode;
mimeparser_yylex_init(&yyscanner);
PARSER_initialize(&pstate, yyscanner);
PARSER_setbuffer(text);
PARSER_setfp(NULL,yyscanner);
res = mimeparser_yyparse(&pstate,yyscanner);
mimeparser_yylex_destroy(yyscanner);
return res;
}
/**
* Parses a file into a MiniMIME context
*
* @param ctx A valid MiniMIME context object
* @param filename The name of the file to parse
* @param parsemode The parsemode
* @param flags The flags to pass to the parser
* @returns 0 on success or -1 on failure
* @note Sets mm_errno if an error occurs
*
* This function parses a MIME message, stored in the filesystem according to
* the parseflags and stores the results in the MiniMIME context specified by
* ctx.
*
* The following modes can be used to specify how the message should be
* parsed:
*
* - MM_PARSE_STRICT: Do not tolerate MIME violations
* - MM_PARSE_LOOSE: Tolerate as much MIME violations as possible
*
* The context needs to be initialized before using mm_context_new() and may
* be freed using mm_context_free().
*/
typedef void *yyscan_t;
int
mm_parse_file(MM_CTX *ctx, const char *filename, int parsemode, int flags)
{
FILE *fp;
int res;
void *yyscanner;
struct parser_state pstate;
int mimeparser_yylex_init (yyscan_t* scanner);
int mimeparser_yylex_destroy (yyscan_t yyscanner );
mimeparser_yylex_init(&yyscanner);
if ((fp = fopen(filename, "r")) == NULL) {
mm_errno = MM_ERROR_ERRNO;
return -1;
}
PARSER_setfp(fp,yyscanner);
PARSER_initialize(&pstate, yyscanner);
pstate.ctx = ctx;
pstate.parsemode = parsemode;
res = mimeparser_yyparse(&pstate,yyscanner);
mimeparser_yylex_destroy(yyscanner);
fclose(fp);
return res;
}
int
mm_parse_fileptr(MM_CTX *ctx, FILE *f, int parsemode, int flags)
{
int res;
void *yyscanner;
struct parser_state pstate;
int mimeparser_yylex_init (yyscan_t* scanner);
int mimeparser_yylex_destroy (yyscan_t yyscanner );
mimeparser_yylex_init(&yyscanner);
PARSER_setfp(f, yyscanner);
PARSER_initialize(&pstate, yyscanner);
pstate.ctx = ctx;
pstate.parsemode = parsemode;
res = mimeparser_yyparse(&pstate,yyscanner);
mimeparser_yylex_destroy(yyscanner);
return res;
}