[8182] | 1 | #include "oaiargs.h"
|
---|
| 2 | #include "cgiutils.h"
|
---|
| 3 |
|
---|
| 4 | // read the arguments from a stream for an OAI action
|
---|
| 5 | void oaiargs::readArgs(istream &in)
|
---|
| 6 | {
|
---|
| 7 | unsigned char buffer[256];
|
---|
| 8 | unsigned char *end = buffer + 256;
|
---|
| 9 | unsigned char *here = end;
|
---|
| 10 | unsigned char *start, *equals;
|
---|
| 11 |
|
---|
| 12 | // if no parameters, return now.
|
---|
| 13 | if (in.eof()) {
|
---|
| 14 | cerr << "Aborting on EOF" << endl;
|
---|
| 15 | return;
|
---|
| 16 | }
|
---|
| 17 |
|
---|
| 18 | // fill the buffer with the stream
|
---|
| 19 | this->fillBuffer(in, buffer, &here, end);
|
---|
| 20 |
|
---|
| 21 | // start at the beginning of the buffer, read
|
---|
| 22 | // the stream until exhausted
|
---|
| 23 | here = buffer;
|
---|
| 24 | while (*here != '\0')
|
---|
| 25 | {
|
---|
| 26 | // each iteration of the outer loop reads a single argument
|
---|
| 27 |
|
---|
| 28 | // we start an argument and continue until the buffer is
|
---|
| 29 | // exhausted or the next argument is found
|
---|
| 30 | start = here;
|
---|
| 31 | equals = NULL;
|
---|
| 32 | while (*here != '&' && *here != '\0') {
|
---|
| 33 | // remember where the equals sign was if necessary
|
---|
| 34 | if (equals == NULL && *here == '=') {
|
---|
| 35 | equals = here;
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | // move to the next character
|
---|
[9608] | 39 | ++here;
|
---|
[8182] | 40 |
|
---|
| 41 | // and refill the buffer as necessary
|
---|
| 42 | if (here == end) {
|
---|
| 43 | this->fillBuffer(in, buffer, &here, end);
|
---|
| 44 | }
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | // we've now got the scope of one argument, where it begins, ends
|
---|
| 48 | // and where the equals sign is; we now split it up accordingly
|
---|
| 49 | // to create the actual text_t type elements for the label and the
|
---|
| 50 | // value.
|
---|
| 51 | text_t label, value;
|
---|
| 52 | if (equals == NULL) {
|
---|
[21424] | 53 | label.setcarr((char *) start, (long) here - (long) start);
|
---|
[8182] | 54 | value = label;
|
---|
| 55 | }
|
---|
| 56 | else {
|
---|
[21424] | 57 | label.setcarr((char *) start, (long) equals - (long) start);
|
---|
[9608] | 58 | ++equals;
|
---|
[21424] | 59 | value.setcarr((char *) equals, (long) here - (long) equals);
|
---|
[8182] | 60 | }
|
---|
| 61 |
|
---|
| 62 | decode_cgi_arg(value);
|
---|
| 63 |
|
---|
| 64 | // output to the screen for tracing purposes
|
---|
| 65 | // cerr << label << "?" << value << endl;
|
---|
| 66 |
|
---|
| 67 | // if the argument already exists, raise an error
|
---|
| 68 | if (this->arguments.count(label) > 0) {
|
---|
| 69 | this->duplicateArg = true;
|
---|
| 70 | }
|
---|
| 71 | else {
|
---|
| 72 | // add this argument to the argument map
|
---|
| 73 | this->arguments[label] = value;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | // don't choke on an argument separator; swallow it so we don't get
|
---|
| 77 | // inifinite loops above
|
---|
| 78 | if (*here == '&') {
|
---|
[9608] | 79 | ++here;
|
---|
[8182] | 80 | }
|
---|
| 81 | }
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | void oaiargs::fillBuffer(istream &in, unsigned char *buffer, unsigned char **currentPtr, unsigned char *end)
|
---|
| 85 | {
|
---|
| 86 | unsigned char *fill = buffer;
|
---|
| 87 |
|
---|
| 88 | // copy out the remaining buffer space
|
---|
| 89 | while (*currentPtr != end) {
|
---|
| 90 | *fill = **currentPtr;
|
---|
[10336] | 91 | *currentPtr++;
|
---|
[8182] | 92 | }
|
---|
| 93 |
|
---|
| 94 | // set the buffer appropriately
|
---|
| 95 | *currentPtr = fill;
|
---|
| 96 |
|
---|
| 97 | // refill the buffer
|
---|
[8228] | 98 | in.get((char *)*currentPtr, (char *)end - (char *)*currentPtr);
|
---|
[8182] | 99 | }
|
---|
| 100 |
|
---|