summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Denker <jsd@av8n.com>2012-11-23 10:05:12 (GMT)
committerJohn Denker <jsd@av8n.com>2012-11-23 10:05:12 (GMT)
commitabc922f8a3f40b5f011cffe5cbe06e2c2df90d02 (patch)
tree22868f53795241b277b1cbc9990a8646795d0413
parenta26e789121448d095227583f7a29ae2deac88a33 (diff)
major cleanup, now handles nested multipart mime
-rw-r--r--tools/libskrewt.c74
-rw-r--r--tools/libskrewt.h9
-rw-r--r--tools/ward.c8
3 files changed, 50 insertions, 41 deletions
diff --git a/tools/libskrewt.c b/tools/libskrewt.c
index dc131f9..595c3fe 100644
--- a/tools/libskrewt.c
+++ b/tools/libskrewt.c
@@ -6,8 +6,9 @@
using namespace std;
void parse_content(const string type_spec_line,
- string &maintype, string &boundary) {
- cerr << "parser called with: " << type_spec_line << endl;
+ string &maintype, string &boundary,
+ const string old_bdy) {
+ if (0) cerr << "parser called with: " << type_spec_line << endl;
string get_type(type_spec_line);
size_t where = get_type.find_first_of(" \t;\n");
@@ -46,7 +47,7 @@ void parse_content(const string type_spec_line,
boundary = boundary.substr(0, where);
}
} else {
- //xxxxxxx cerr << "boundary= not found in " << type_spec_line << endl;
+ boundary = old_bdy;
}
}
@@ -56,7 +57,7 @@ int skrewt::krunch_rfrom(){
string word;
parse >> word;
if (word != "from") {
- cerr << progid << " bad 'Received: from' line ... '"
+ cerr << progid << " bad 'Received: from' line; no from, instead '"
<< word << "'" << endl;
return ex_syserr;
}
@@ -136,8 +137,8 @@ xstr skrewt::getRecord(istream& xin){
<< " folded header rejection: bigger than " << maxsize << endl;
exeunt(ex_spam);
}
- bigbuf.push_back(line.str);
- headrec += "\n" + noCR(line.str);
+ bigbuf.push_back(more.str);
+ headrec += "\n" + noCR(more.str);
}
return {0, headrec};
}
@@ -202,7 +203,7 @@ int skrewt::headers(istream& xin){
return 0;
}
-int skrewt::dump_headers(std::ostream& xout){
+int skrewt::dump_bigbuf(std::ostream& xout){
for (vector<string>::const_iterator foo = bigbuf.begin();
foo != bigbuf.end(); foo++){
xout << *foo << endl;
@@ -394,11 +395,21 @@ public:
string bdy;
};
+void dump(const list<conner> sitch){
+ int depth = sitch.size();
+ cerr << " depth: " << depth;
+ if (depth){
+ cerr << " tp: " << sitch.front().tp
+ << " bdy: " << sitch.front().bdy;
+ }
+ cerr << endl;
+}
+
int skrewt::body(std::istream& xin, std::ostream& xout){
list<conner> sitch;
if (content_type.length()) {
string tp, bdy;
- parse_content(content_type, tp, bdy);
+ parse_content(content_type, tp, bdy, "");
sitch.push_front({tp, bdy});
} else {
// assume single-part, all text if not otherwise specified:
@@ -409,10 +420,8 @@ int skrewt::body(std::istream& xin, std::ostream& xout){
// Now spew the rest of the message
// Note that multipart messages can have sub-headers.
- cerr << "body begins: tp: " << sitch.front().tp
- << " bdy: " << sitch.front().bdy
- << " depth: " << sitch.size()
- << endl;
+ cerr << "body begins: ";
+ dump(sitch);
int in_subheads(0);
int textlines(0);
@@ -420,29 +429,20 @@ int skrewt::body(std::istream& xin, std::ostream& xout){
for (;;){ // outer loop over all lines in the body
if (xin.eof()) break;
if (xin.bad()) return 1;
- string line;
-// on fail, go back to top of outer loop and check for eof versus bad
- if (getline(xin, line).fail()) continue;
- msgsize += line.length()+1;
- if (msgsize > maxsize) {
- cerr << progid << " rejection: bigger than " << maxsize << endl;
- maybe_exeunt(ex_spam, error_exit);
- }
- bigbuf.push_back(line);
- xout << line << endl;
+ xstr rec = getRecord(xin);
+ if (rec.err) break;
+ string line = rec.str;
if (0) cerr << "+++" << line << endl;
if (in_subheads){
if (line == "" || line == "\r") {
in_subheads = 0;
cerr << "+++ end of subhead" << endl;
string tp, bdy;
- parse_content(sub_conntype, tp, bdy);
- cerr << tp << " '" << bdy << "'" << endl;
+ parse_content(sub_conntype, tp, bdy, sitch.front().bdy);
sitch.push_front({tp, bdy});
- cerr << "payload mode: tp: " << sitch.front().tp
- << " bdy: " << sitch.front().bdy
- << " depth: " << sitch.size()
- << endl;
+
+ cerr << "payload mode: ";
+ dump(sitch);
}
}
if (/* still */in_subheads){
@@ -463,20 +463,24 @@ int skrewt::body(std::istream& xin, std::ostream& xout){
sub_conntype = rest;
}
} else {
- if (sitch.front().tp == "multipart"
+ if (sitch.front().bdy != ""
&& line == "--" + sitch.front().bdy) {
- cerr << "boundary: begin subhead block" << endl;
+ if (sitch.front().tp != "multipart")
+ sitch.pop_front();
+ cerr << "boundary: begin subhead block ";
+ dump(sitch);
in_subheads = 1;
sub_conntype = "";
continue;
}
- if (sitch.front().tp == "multipart"
+ if (sitch.front().bdy != ""
&& line == "--" + sitch.front().bdy + "--") {
+ string dead_bdy = sitch.front().bdy;
sitch.pop_front();
- cerr << "found subhead termination --> "
- << sitch.front().tp
- << endl;
- in_subheads = 1;
+ if (sitch.front().bdy == dead_bdy) sitch.pop_front();
+ cerr << "boundary: termination ";
+ dump(sitch);
+ in_subheads = 0;
continue;
}
if (sitch.front().tp == "text") textlines++;
diff --git a/tools/libskrewt.h b/tools/libskrewt.h
index 5ae6ece..34cdfd3 100644
--- a/tools/libskrewt.h
+++ b/tools/libskrewt.h
@@ -31,30 +31,31 @@ public:
std::string content_type;
std::list<std::string> delivered_to;
int msgsize;
- std::vector<std::string> bigbuf;
int saw_blank_line;
int recno;
-
int maxsize;
int error_exit;
int mid_required;
+ std::vector<std::string> headerbuf;
+ std::vector<std::string> bigbuf;
+ xstr lookahead;
// constructor
skrewt()
: spf_result(SPF_RESULT_INVALID),
boundary("x-xx-x"), msgsize(0), saw_blank_line(0), recno(0),
maxsize(1000*1000), error_exit(0), mid_required(0),
+ headerbuf(0), bigbuf(0),
lookahead(1, "")
{}
xstr getRecord(std::istream& xin);
xstr getLine(std::istream& xin);
int headers(std::istream& xin);
- int dump_headers(std::ostream& xout);
+ int dump_bigbuf(std::ostream& xout);
int interstage();
int body(std::istream& xin, std::ostream& xout);
int krunch_rfrom();
- xstr lookahead;
};
void parse_content(const std::string type_spec_line,
diff --git a/tools/ward.c b/tools/ward.c
index b8bdee4..abea289 100644
--- a/tools/ward.c
+++ b/tools/ward.c
@@ -117,7 +117,9 @@ int main(int _argc, const char** _argv){
int rslt = mysk.headers(cin);
if (rslt) return rslt;
- mysk.dump_headers(cout);
+ mysk.dump_bigbuf(cout);
+ mysk.headerbuf = mysk.bigbuf;
+ mysk.bigbuf = vector<string>(0);
// Headers are done.
// Do some early-stage thinking.
@@ -126,5 +128,7 @@ int main(int _argc, const char** _argv){
if (rslt) return rslt;
rslt = mysk.body(cin, cout);
- return rslt;
+ if (rslt) return rslt;
+ mysk.dump_bigbuf(cout);
+ return 0;
}