3 #include "util_const.h"
4 #include "util_check.h"
5 #include "util_string.h"
6 #include <boost/lexical_cast.hpp>
12 static bool pcmp (char** a, const char* b, int n) {
13 int rc = strncmp (*a, b, n);
21 #define cmp(a,b) pcmp (&(a), (b), sizeof (b) - 1)
22 #define zcmp(a,b) (strcmp ((a), (b)) == 0)
31 |post-file-html:FILENAME|
36 |post-file-ml:FILENAME|
41 |post-file-limit:DATASIZE|
45 |frame-options:[DENY|SAMEORIGIN]|
50 void AppEnv::readOption (int argc, char** argv, MotorEnv* env) {
54 arg0 = ustring (argv[0]);
60 for (i = 1; i < argc; i ++) {
62 if (cmp (p, "datastore:")) {
63 datastore = ustring (p);
64 if (! checkName (datastore))
65 throw (datastore + uErrorBadDatastore);
66 } else if (cmp (p, "get-html:")) {
67 getHtml = ustring (p);
68 if (getHtml != uDash && ! checkResourceName (getHtml))
69 throw (getHtml + uErrorBadFile);
70 } else if (cmp (p, "post-html:")) {
71 postHtml = ustring (p);
72 if (postHtml != uDash && ! checkResourceName (postHtml))
73 throw (postHtml + uErrorBadFile);
74 } else if (cmp (p, "post-file-html:")) {
75 postFileHtml = ustring (p);
76 if (postFileHtml != uDash && ! checkResourceName (postFileHtml))
77 throw (postFileHtml + uErrorBadFile);
78 } else if (cmp (p, "html:")) {
79 postFileHtml = postHtml = getHtml = ustring (p);
80 if (getHtml != uDash && ! checkResourceName (getHtml))
81 throw (getHtml + uErrorBadFile);
82 } else if (cmp (p, "error-html:")) {
83 errorHtml = ustring (p);
84 if (errorHtml != uDash && ! checkResourceName (errorHtml))
85 throw (errorHtml + uErrorBadFile);
86 } else if (cmp (p, "get-ml:")) {
88 if (getML != uDash && ! checkResourceName (getML))
89 throw (getML + uErrorBadFile);
90 } else if (cmp (p, "post-ml:")) {
92 if (postML != uDash && ! checkResourceName (postML))
93 throw (postML + uErrorBadFile);
94 } else if (cmp (p, "post-file-ml:")) {
95 postFileML = ustring (p);
96 if (postFileML != uDash && ! checkResourceName (postFileML))
97 throw (postFileML + uErrorBadFile);
98 } else if (cmp (p, "ml:")) {
99 postML = getML = ustring (p);
100 if (getML != uDash && ! checkResourceName (getML))
101 throw (getML + uErrorBadFile);
102 } else if (cmp (p, "type:")) {
103 mimetype = ustring (p);
104 if (! checkMimeType (mimetype))
105 throw (mimetype + ": bad mime type.");
106 } else if (cmp (p, "to-code:")) {
108 if (! checkName (ocode))
109 throw (ocode + ": bad encoding name.");
110 } else if (cmp (p, "post-limit:")) {
111 postlimit = boost::lexical_cast <size_t> (p);
113 postlimit = cPOSTLIMITDEFAULT;
114 } else if (postlimit > cPOSTLIMITHARD) {
115 *env->log << "post-limit:" << postlimit << ": limit exceeded.\n";
116 postlimit = cPOSTLIMITHARD;
118 } else if (cmp (p, "postfile-limit:")
119 || cmp (p, "post-file-limit:")) {
120 postfilelimit = boost::lexical_cast <size_t> (p);
121 if (postfilelimit < 0) {
122 postfilelimit = cPOSTFILELIMITDEFAULT;
123 } else if (postfilelimit > cPOSTFILELIMITHARD) {
124 *env->log << "post-file-limit:" << postfilelimit << ": limit exceeded.\n";
125 postfilelimit = cPOSTFILELIMITHARD;
127 } else if (zcmp (p, "random-cookie")) {
128 cacheControl = CC_COOKIE;
129 } else if (zcmp (p, "random-url")) {
130 cacheControl = CC_URL;
131 } else if (zcmp (p, "no-cache")) {
132 cacheControl = CC_NOCACHE;
133 } else if (cmp (p, "frame-options:")) {
134 if (zcmp (p, "DENY")) {
135 frameOpt = FOPT_DENY;
136 } else if (zcmp (p, "SAMEORIGIN")) {
137 frameOpt = FOPT_SAMEORIGIN;
139 throw (ustring (CharConst ("frame-options:")) + p + ustring (CharConst (" bad option.")));
141 } else if (zcmp (p, "dump")) {
143 } else if (p[0] == ';' || p[0] == '(' || p[0] == '<') {
146 throw (ustring (p) + ustring (": unrecognized parameter"));
151 void AppEnv::setDefault () {
152 if (mimetype.size () == 0)
153 mimetype = ustring (CharConst (kMIME_HTML));
154 if (cacheControl == CC_NONE)
155 cacheControl = CC_COOKIE;
158 ustring AppEnv::scriptName () {
159 return getenvString (kSCRIPT_FILENAME);
162 void AppEnv::dump (std::ostream& out) {
164 if (datastore.size () > 0)
165 out << " datastore:" << datastore << "\n";
166 if (getML.size () > 0) {
167 if (getML == postML) {
168 out << " ml:" << getML << "\n";
170 out << " get-ml:" << getML << "\n";
171 if (postML.size () > 0)
172 out << " post-ml:" << postML << "\n";
174 } else if (postML.size () > 0) {
175 out << " post-ml:" << postML << "\n";
177 if (postFileML.size () > 0) {
178 out << " post-file-ml:" << postFileML << "\n";
180 if (getHtml.size () > 0) {
181 if (getHtml == postHtml) {
182 out << " html:" << getHtml << "\n";
184 out << " get-html:" << getHtml << "\n";
185 if (postHtml.size () > 0)
186 out << " post-html:" << postHtml << "\n";
188 } else if (postHtml.size () > 0) {
189 out << " post-html:" << postHtml << "\n";
191 if (postFileHtml.size () > 0) {
192 out << " post-file-html:" << postFileHtml << "\n";
194 if (errorHtml.size () > 0)
195 out << " error-html:" << errorHtml << "\n";
196 if (mimetype.size () > 0)
197 out << " type:" << mimetype << "\n";
198 if (postlimit != cPOSTLIMITDEFAULT)
199 out << " post-limit:" << postlimit << "\n";
200 if (postfilelimit != cPOSTFILELIMITDEFAULT)
201 out << " post-file-limit:" << postfilelimit << "\n";
202 switch (cacheControl) {
204 out << " random-cookie\n";
207 out << " random-url\n";
210 out << " no-cache\n";
217 out << " frame-options:DENY\n";
219 case FOPT_SAMEORIGIN:
220 out << " frame-options:SAMEORIGIN\n";
223 out << " frame-options:ALLOW-FROM " << foptUri << "\n";
227 #ifndef TARGET_MLDUMP
233 void AppEnv::setErrorLog (const ustring& path, bool fappend) {
234 int flag = O_WRONLY | O_CREAT;
239 open (path.c_str (), flag, 0666);