open-source-search-engine/Mime.cpp

89 lines
2.3 KiB
C++
Raw Normal View History

2013-08-03 00:12:24 +04:00
#include "gb-include.h"
#include "Mime.h"
#include "Mem.h"
// . returns false if could not get a valid mime
// . we need the url in case there's a Location: mime that's base-relative
2014-11-11 01:45:11 +03:00
void Mime::set ( char *mime , int32_t mimeLen ) {
2013-08-03 00:12:24 +04:00
m_mime = mime;
m_mimeLen = mimeLen;
m_mimeEnd = mime + mimeLen;
}
2014-11-11 01:45:11 +03:00
char *Mime::getValue ( char *field , int32_t *valueLen ) {
2013-08-03 00:12:24 +04:00
// caller's field length
2014-11-11 01:45:11 +03:00
int32_t fieldLen = gbstrlen ( field );
2013-08-03 00:12:24 +04:00
// parms to getLine()
char *f , *v;
2014-11-11 01:45:11 +03:00
int32_t flen , vlen;
2013-08-03 00:12:24 +04:00
char *line = m_mime;
// keep getting lines from the mime
while ( ( line = getLine ( line , &f, &flen, &v , &vlen ) ) ) {
if ( flen != fieldLen ) continue;
if ( strncasecmp ( f , field , flen ) != 0 ) continue;
*valueLen = vlen;
return v;
}
// return NULL if no value found for "field"
return NULL;
}
// . return ptr to next line to try
// . return NULL if no lines left
char *Mime::getLine ( char *line ,
2014-11-11 01:45:11 +03:00
char **field , int32_t *fieldLen ,
char **value , int32_t *valueLen ) {
2013-08-03 00:12:24 +04:00
// reset field and value lengths
*fieldLen = 0;
*valueLen = 0;
// a NULL line means the start
if ( line == NULL ) line = m_mime;
// a simple ptr
char *p = line;
char *pend = m_mimeEnd;
loop:
// skip to next field (break on comment)
while ( p < pend && *p!='#' && !is_alnum_a(*p) ) p++;
// bail on EOF
if ( p >= pend ) return NULL;
// point to next line if comment
if ( *p == '#' ) {
while ( p < pend && *p != '\n' && *p !='\r' ) p++;
// NULL on EOF
if ( p >= pend ) return NULL;
// try to get another line
goto loop;
}
// save p's position
char *s = p;
// continue until : or \n or \r or EOF
while ( p < pend && *p != ':' && *p != '\n' && *p !='\r' ) p++;
// NULL on EOF
if ( p >= pend ) return NULL;
// return p for getting next line if no : found
if ( *p != ':' ) goto loop;
// set the field
*field = s;
// set the field length
*fieldLen = p - s;
// reset value length to 0, in case we find none
*valueLen = 0;
// skip over :
p++;
// skip normal spaces and tabs at p now
while ( p < pend && (*p==' ' || *p=='\t') ) p++;
// NULL on EOF
if ( p >= pend ) return NULL;
// value is next
*value = p;
// goes till we hit \r or \n
while ( p < pend && *p != '\n' && *p !='\r' ) p++;
// set value length
*valueLen = p - *value;
// NULL on EOF
if ( p >= pend ) return NULL;
// otherwise, p is start of next line
return p;
}