This repository has been archived by the owner on Oct 1, 2018. It is now read-only.
/
ActionParser.h
355 lines (267 loc) · 13.7 KB
/
ActionParser.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
//
// ActionParser.h
// snowcrash
//
// Created by Zdenek Nemec on 5/4/13.
// Copyright (c) 2013 Apiary Inc. All rights reserved.
//
#ifndef SNOWCRASH_ACTIONPARSER_H
#define SNOWCRASH_ACTIONPARSER_H
#include "HTTP.h"
#include "SectionParser.h"
#include "ParametersParser.h"
#include "PayloadParser.h"
#include "RegexMatch.h"
namespace snowcrash {
/** Nameless action matching regex */
const char* const ActionHeaderRegex = "^[[:blank:]]*" HTTP_REQUEST_METHOD "[[:blank:]]*" URI_TEMPLATE "?$";
/** Named action matching regex */
const char* const NamedActionHeaderRegex = "^[[:blank:]]*" SYMBOL_IDENTIFIER "\\[" HTTP_REQUEST_METHOD "]$";
/** Internal type alias for Collection of Action */
typedef Collection<Action>::type Actions;
typedef Collection<Action>::const_iterator ActionIterator;
// Method signature
enum ActionSignature {
NoActionSignature = 0,
MethodActionSignature, // # GET
MethodURIActionSignature, // # GET /uri
NamedActionSignature, // # My Method [GET]
UndefinedActionSignature = -1
};
/**
* Action Section processor
*/
template<>
struct SectionProcessor<Action> : public SectionProcessorBase<Action> {
static MarkdownNodeIterator processSignature(const MarkdownNodeIterator& node,
SectionParserData& pd,
Report& report,
Action& out) {
mdp::ByteBuffer signature, remainingContent;
CaptureGroups captureGroups;
signature = GetFirstLine(node->text, remainingContent);
if (RegexCapture(signature, ActionHeaderRegex, captureGroups, 3)) {
out.method = captureGroups[1];
} else if (RegexCapture(signature, NamedActionHeaderRegex, captureGroups, 3)) {
out.name = captureGroups[1];
out.method = captureGroups[2];
TrimString(out.name);
}
if (!remainingContent.empty()) {
out.description += remainingContent;
}
return ++MarkdownNodeIterator(node);
}
static MarkdownNodeIterator processNestedSection(const MarkdownNodeIterator& node,
const MarkdownNodes& siblings,
SectionParserData& pd,
Report& report,
Action& out) {
SectionType sectionType = pd.sectionContext();
MarkdownNodeIterator cur = node;
Payload payload;
std::stringstream ss;
mdp::CharactersRangeSet sourceMap = mdp::BytesRangeSetToCharactersRangeSet(node->sourceMap, pd.sourceData);
switch (sectionType) {
case ParametersSectionType:
return ParametersParser::parse(node, siblings, pd, report, out.parameters);
case RequestSectionType:
case RequestBodySectionType:
cur = PayloadParser::parse(node, siblings, pd, report, payload);
if (out.examples.empty() || !out.examples.back().responses.empty()) {
TransactionExample transaction;
out.examples.push_back(transaction);
}
checkPayload(sectionType, sourceMap, payload, out, report);
out.examples.back().requests.push_back(payload);
break;
case ResponseSectionType:
case ResponseBodySectionType:
cur = PayloadParser::parse(node, siblings, pd, report, payload);
if (out.examples.empty()) {
TransactionExample transaction;
out.examples.push_back(transaction);
}
checkPayload(sectionType, sourceMap, payload, out, report);
out.examples.back().responses.push_back(payload);
break;
case BodySectionType:
case SchemaSectionType:
// TODO: Can improve this
// WARN: Ignoring section
ss << "Ignoring " << SectionName(sectionType) << " list item, ";
ss << SectionName(sectionType) << " list item is expected to be indented by 4 spaces or 1 tab";
report.warnings.push_back(Warning(ss.str(),
IgnoringWarning,
sourceMap));
cur = ++MarkdownNodeIterator(node);
break;
default:
break;
}
return cur;
}
static MarkdownNodeIterator processUnexpectedNode(const MarkdownNodeIterator& node,
const MarkdownNodes& siblings,
SectionParserData& pd,
SectionType& sectionType,
Report& report,
Action& out) {
if ((node->type == mdp::ParagraphMarkdownNodeType ||
node->type == mdp::CodeMarkdownNodeType) &&
sectionType == ResponseBodySectionType &&
!out.examples.empty() &&
!out.examples.back().responses.empty()) {
mdp::ByteBuffer asset = mdp::MapBytesRangeSet(node->sourceMap, pd.sourceData);
if (asset[asset.length() - 1] != '\n') {
asset += "\n";
}
if (asset[asset.length() - 2] != '\n') {
asset += "\n";
}
out.examples.back().responses.back().body += asset;
if (node->type == mdp::ParagraphMarkdownNodeType) {
// WARN: Ignoring unexpected node
std::stringstream ss;
ss << "Danling message-body asset, expected a pre-formatted code block, ";
ss << "indent every one of it's line by 8 spaces or 2 tabs";
mdp::CharactersRangeSet sourceMap = mdp::BytesRangeSetToCharactersRangeSet(node->sourceMap, pd.sourceData);
report.warnings.push_back(Warning(ss.str(),
IndentationWarning,
sourceMap));
}
} else {
// WARN: Ignoring unexpected node
mdp::CharactersRangeSet sourceMap = mdp::BytesRangeSetToCharactersRangeSet(node->sourceMap, pd.sourceData);
report.warnings.push_back(Warning("ignoring unrecognized block",
IgnoringWarning,
sourceMap));
}
return ++MarkdownNodeIterator(node);
}
static SectionType sectionType(const MarkdownNodeIterator& node) {
if (node->type == mdp::HeaderMarkdownNodeType
&& !node->text.empty()) {
mdp::ByteBuffer subject = node->text;
TrimString(subject);
if (RegexMatch(subject, ActionHeaderRegex) ||
RegexMatch(subject, NamedActionHeaderRegex)) {
return ActionSectionType;
}
}
return UndefinedSectionType;
}
static SectionType nestedSectionType(const MarkdownNodeIterator& node) {
SectionType nestedType = UndefinedSectionType;
// Check if parameters section
nestedType = SectionProcessor<Parameters>::sectionType(node);
if (nestedType != UndefinedSectionType) {
return nestedType;
}
// Check if payload section
nestedType = SectionProcessor<Payload>::sectionType(node);
if (nestedType != UndefinedSectionType) {
return nestedType;
}
// Check if asset section (dangling)
nestedType = SectionProcessor<Asset>::sectionType(node);
if (nestedType != UndefinedSectionType) {
return nestedType;
}
return UndefinedSectionType;
}
static void finalize(const MarkdownNodeIterator& node,
SectionParserData& pd,
Report& report,
Action& out) {
if (out.examples.empty()) {
// WARN: No response for action
mdp::CharactersRangeSet sourceMap = mdp::BytesRangeSetToCharactersRangeSet(node->sourceMap, pd.sourceData);
report.warnings.push_back(Warning("action is missing a response",
EmptyDefinitionWarning,
sourceMap));
} else if (!out.examples.empty() &&
!out.examples.back().requests.empty() &&
out.examples.back().responses.empty()) {
// WARN: No response for request
std::stringstream ss;
ss << "action is missing a response for ";
if (out.examples.back().requests.back().name.empty()) {
ss << "a request";
} else {
ss << "the '" << out.examples.back().requests.back().name << "' request";
}
mdp::CharactersRangeSet sourceMap = mdp::BytesRangeSetToCharactersRangeSet(node->sourceMap, pd.sourceData);
report.warnings.push_back(Warning(ss.str(),
EmptyDefinitionWarning,
sourceMap));
}
}
/**
* \brief Check & report payload validity.
* \param sectionType A section of the payload.
* \param sourceMap Payload signature source map.
* \param payload The payload to be checked.
* \param action The Action to which payload belongs to.
* \param report Parser report.
*/
static void checkPayload(SectionType sectionType,
const mdp::CharactersRangeSet sourceMap,
const Payload& payload,
const Action& action,
Report& report) {
if (isPayloadDuplicate(sectionType, payload, action.examples.back())) {
// WARN: Duplicate payload
std::stringstream ss;
ss << SectionName(sectionType) << " payload `" << payload.name << "`";
ss << " already defined for `" << action.method << "` method";
report.warnings.push_back(Warning(ss.str(),
DuplicateWarning,
sourceMap));
}
if (sectionType == ResponseSectionType || sectionType == ResponseBodySectionType) {
HTTPStatusCode code;
if (!payload.name.empty()) {
std::stringstream(payload.name) >> code;
}
HTTPMethodTraits methodTraits = GetMethodTrait(action.method);
if (!methodTraits.allowBody && !payload.body.empty()) {
// WARN: Edge case for 2xx CONNECT
if (action.method == HTTPMethodName::Connect && code/100 == 2) {
std::stringstream ss;
ss << "the response for " << code << " " << action.method << " request MUST NOT include a " << SectionName(BodySectionType);
report.warnings.push_back(Warning(ss.str(),
EmptyDefinitionWarning,
sourceMap));
} else if (action.method != HTTPMethodName::Connect && !methodTraits.allowBody) {
std::stringstream ss;
ss << "the response for " << action.method << " request MUST NOT include a " << SectionName(BodySectionType);
report.warnings.push_back(Warning(ss.str(),
EmptyDefinitionWarning,
sourceMap));
}
return;
}
}
}
/**
* Checks whether given section payload has duplicate within its transaction examples
* \return True when a duplicate is found, false otherwise.
*/
static bool isPayloadDuplicate(SectionType& sectionType,
const Payload& payload,
const TransactionExample& example) {
if (sectionType == RequestSectionType) {
Collection<Request>::const_iterator duplicate = FindRequest(example, payload);
return duplicate != example.requests.end();
} else if (sectionType == ResponseSectionType) {
Collection<Response>::const_iterator duplicate = FindResponse(example, payload);
return duplicate != example.responses.end();
}
return false;
}
};
/** Action Section Parser */
typedef SectionParser<Action, HeaderSectionAdapter> ActionParser;
}
#endif