Bincimap 2.0.17
Easy Imapping
Loading...
Searching...
No Matches
mime-parseonlyheader.cc
Go to the documentation of this file.
1
7#include "mime.h"
8#include "mime-utils.h"
9#include "mime-inputsource.h"
10#include "convert.h"
11#include <string>
12#include <vector>
13#include <map>
14#include <exception>
15#include <iostream>
16
17#include <string.h>
18#include <ctype.h>
19#include <stdio.h>
20#include <errno.h>
21
22using namespace ::std;
23
24//------------------------------------------------------------------------
26{
27 if (allIsParsed || headerIsParsed)
28 return;
29
30 headerIsParsed = true;
31
32 if (!mimeSource || mimeSource->getFileDescriptor() != fd) {
33 delete mimeSource;
35 } else {
37 }
38
39
41 headerlength = 0;
43 bodylength = 0;
44 messagerfc822 = false;
45 multipart = false;
46
47 nlines = 0;
48 nbodylines = 0;
49
51 delete mimeSource;
52}
53
54//------------------------------------------------------------------------
55int Binc::MimePart::parseOnlyHeader(const string &toboundary) const
56{
57 string name;
58 string content;
59 char cqueue[4];
60 memset(cqueue, 0, sizeof(cqueue));
61
62 headerstartoffsetcrlf = mimeSource->getOffset();
63
64 bool quit = false;
65 char c = '\0';
66
67 while (!quit) {
68 // read name
69 while (1) {
70 if (!mimeSource->getChar(&c)) {
71 quit = true;
72 break;
73 }
74
75 if (c == '\n') ++nlines;
76 if (c == ':') break;
77 if (c == '\n') {
78 for (int i = name.length() - 1; i >= 0; --i)
80
81 quit = true;
82 name = "";
83 break;
84 }
85
86 name += c;
87
88 if (name.length() == 2 && name.substr(0, 2) == "\r\n") {
89 name = "";
90 quit = true;
91 break;
92 }
93 }
94
95 if (name.length() == 1 && name[0] == '\r') {
96 name = "";
97 break;
98 }
99
100 if (quit) break;
101
102 while (!quit) {
103 if (!mimeSource->getChar(&c)) {
104 quit = true;
105 break;
106 }
107
108 if (c == '\n') ++nlines;
109
110 for (int i = 0; i < 3; ++i)
111 cqueue[i] = cqueue[i + 1];
112
113 cqueue[3] = c;
114 if (strncmp(cqueue, "\r\n\r\n", 4) == 0) {
115 quit = true;
116 break;
117 }
118
119 if (cqueue[2] == '\n') {
120 // guess the mime rfc says what can not appear on the beginning
121 // of a line.
122 if (!isspace(cqueue[3])) {
123 if (content.length() > 2)
124 content.resize(content.length() - 2);
125
126 trim(content);
127 h.add(name, content);
128 name = c;
129 content = "";
130 break;
131 }
132 }
133
134 content += c;
135 }
136 }
137
138 if (name != "") {
139 if (content.length() > 2)
140 content.resize(content.length() - 2);
141 h.add(name, content);
142 }
143
144 headerlength = mimeSource->getOffset() - headerstartoffsetcrlf;
145
146 return 1;
147}
void parseOnlyHeader(int fd) const
virtual void reset(void)
int getFileDescriptor(void) const
unsigned int getOffset(void) const
unsigned int headerstartoffsetcrlf
Definition: mime.h:57
unsigned int nbodylines
Definition: mime.h:63
bool multipart
Definition: mime.h:52
bool messagerfc822
Definition: mime.h:53
virtual int parseOnlyHeader(const std::string &toboundary) const
unsigned int bodylength
Definition: mime.h:61
unsigned int bodystartoffsetcrlf
Definition: mime.h:60
unsigned int headerlength
Definition: mime.h:58
unsigned int nlines
Definition: mime.h:62
Declaration of miscellaneous convertion functions.
The base class of the MIME input source.
Binc::MimeInputSource * mimeSource
Declaration of main mime parser components.
void trim(std::string &s_in, const std::string &chars=" \t\r\n")
Definition: convert.h:137