/*****************************************************************************/
/*
                                   body.h
*/
/*****************************************************************************/

#ifndef BODY_H_LOADED
#define BODY_H_LOADED 1

#include "wasd.h"

/**********/
/* macros */
/**********/

#define BODY_PROCESSED_AS_NONE                0
#define BODY_PROCESSED_AS_TEXT                1
#define BODY_PROCESSED_AS_OTHER               2
#define BODY_PROCESSED_AS_VIRTUALBLOCK        3
#define BODY_PROCESSED_AS_URLENCODED          4
#define BODY_PROCESSED_AS_MULTIPART_FORMDATA  5
#define BODY_PROCESSED_AS_CHUNKED             6

#define CHUNK_STATE_BEGIN   0   /* uninitialized */
#define CHUNK_STATE_SIZE    1   /* getting the chunk size from hex string */
#define CHUNK_STATE_EOL     2   /* finding the CR-LF trailing hex string */
#define CHUNK_STATE_DATA    3   /* processing the chunk data */
#define CHUNK_STATE_EOD     4   /* finding the CR-LF trailing the data */
#define CHUNK_STATE_TRAILER 5   /* end of chunks, processing the trailer */
#define CHUNK_STATE_END     6   /* end of chunks and trailer */

/**************/
/* structures */
/**************/

typedef struct BodyProcessStruct BODY_PROCESS;

struct  BodyProcessStruct
{
   BOOL  /* document posted is URL-encoded */
         ContentTypeUrlEncoded,
         /* form field CDATA contains '^' substituted for LFs */
         UrlEncodedHiddenLF,
         /* delete file after it's been transfered */
         PreviewOnly,
         /* currently processing a hex-encoded field value */
         UrlEncodedFieldHexEncoded,
         /* the current field name has reserved usage */
         UrlEncodedFieldNameReserved;

   int  /* count of bytes in buffer used for block I/O */
        BlockBufferCount,
        /* size in bytes of buffer used for block I/O */
        BlockBufferSize,
        /* contains the VMS status of the contents of the block buffer */
        BlockBufferStatus,
        /* number of the leading 512 byte block */
        BlockBufferVBN,
        /* bytes of data beyond the last whole virtual block */
        BlockLeftOverCount,
        /* position in 'BoundaryBuffer' */
        BoundaryIdx,
        /* final count of characters in buffered MIME header */
        MimeHeaderCount,
        /* when buffering the MIME header */
        MimeHeaderIdx,
        /* RMS multiblock count value */
        MultiBlockCount,
        /* length of MIME boundary used to delimit mixed subparts in buffer */
        MultipartBoundaryLength,
        /* current number of bytes in multipart field being buffered to */
        MultipartFormDataCount,
        /* keep track of 'form-data; name="name" filename=\"..\"' */
        MultipartFormDataFileNameCount,
        /* size of current multipart field being buffered to */
        MultipartFormDataSize,
        /* position in 'HexDecode' */
        UrlDecodeIdx,
        /* position in 'UrlDecode' */
        HexDecodeIdx,
        /* progressive number of bytes processed and passed on */
        ProcessedByteCount,
        /* FAB record attributes supplied with file body */
        UrlEncodedFabRat,
        /* FAB record format supplied with file body */
        UrlEncodedFabRfm,
        /* current length of field name while processing URL-encoded file */
        UrlEncodedFieldNameCount,
        /* size of field name while processing URL-encoded file */
        UrlEncodedFieldNameSize,
        /* current length of field value while processing URL-encoded file */
        UrlEncodedFieldValueCount,
        /* size of field value while processing URL-encoded file */
        UrlEncodedFieldValueSize;

   char  /* points to the start of buffer used for block I/O */
         *BlockBufferPtr,
         /* points to data beyond the last whole virtual block */
         *BlockLeftOverPtr,
         /* multipart content-type */
         *MultipartContentTypePtr,
         /* pointer to start of multipart field being buffered to */
         *MultipartFormDataPtr,
         /* pointer to next character of multipart field being buffered to */
         *MultipartFormDataCurrentPtr;

   char  /* multipart boundary string */
         BoundaryBuffer [96],
         /* should be "ascii" or "binary" */
         FtpFileType [8],
         /* buffers when hex-encode decoding between reads */
         HexDecode [2],
         /* buffer the MIME part header */
         MimeHeaderBuffer [1024],
         /* multipart boundary string (from content-type) */
         MultipartBoundary [96],
         /* buffer the 'form-data; name="uploadfilename"' data */
         MultipartUploadFileName [96],
         /* buffer the 'form-data; filename=".."' data */
         MultipartFileName [96],
         /* used to buffer the 'form-data; name="success"' data */
         MultipartSuccessUrl [256],
         /* buffer a string providing file protection */
         ProtectionHexString [8],
         /* buffer URL-encoded character */
         UrlDecode [4],
         /* store field name while processing URL-encoded file */
         UrlEncodedFieldName [256],
         /* store field value while processing URL-encoded file */
         UrlEncodedFieldValue [256];
};

/***********************/
/* function prototypes */
/***********************/

void BodyBufferAllocate (REQUEST_STRUCT*);
void BodyBufferEndOfFile (REQUEST_STRUCT*);
void BodyBufferWrite (REQUEST_STRUCT*);
BOOL BodyContentGzip (REQUEST_STRUCT*);
int BodyGetMimeBoundary (REQUEST_STRUCT*);
void BodyProcessByVirtualBlock (REQUEST_STRUCT*);
void BodyProcessMultipartFormData (REQUEST_STRUCT*);
int BodyProcessMultipartMimeHeader (REQUEST_STRUCT*);
void BodyProcessReadAll (REQUEST_STRUCT*);
void BodyProcessUrlEncoded (REQUEST_STRUCT*);
void BodyRead (REQUEST_STRUCT*);
void BodyReadAst (REQUEST_STRUCT*);
void BodyReadBegin (REQUEST_STRUCT*, REQUEST_AST, REQUEST_AST);
void BodyReadProcess (REQUEST_STRUCT*);
BOOL BodyReadUnEncode (REQUEST_STRUCT*);
void BodyTransferChunked (REQUEST_STRUCT*);
int BodyTransferChunkedTrailer (REQUEST_STRUCT*, char*, int);

#endif /* BODY_H_LOADED */

/*****************************************************************************/

