libcouchbase 0.2.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +1 -1
- data/README.md +1 -1
- data/ext/libcouchbase/CMakeLists.txt +8 -6
- data/ext/libcouchbase/README.markdown +2 -2
- data/ext/libcouchbase/RELEASE_NOTES.markdown +0 -86
- data/ext/libcouchbase/cmake/Modules/ConfigureDtrace.cmake +0 -11
- data/ext/libcouchbase/cmake/Modules/GenerateConfigDotH.cmake +0 -2
- data/ext/libcouchbase/cmake/Modules/GetLibcouchbaseFlags.cmake +1 -2
- data/ext/libcouchbase/cmake/Modules/GetVersionInfo.cmake +3 -3
- data/ext/libcouchbase/cmake/config-cmake.h.in +0 -2
- data/ext/libcouchbase/cmake/defs.mk.in +2 -0
- data/ext/libcouchbase/cmake/source_files.cmake +5 -21
- data/ext/libcouchbase/include/libcouchbase/auth.h +0 -10
- data/ext/libcouchbase/include/libcouchbase/cntl.h +1 -27
- data/ext/libcouchbase/include/libcouchbase/error.h +1 -15
- data/ext/libcouchbase/include/libcouchbase/n1ql.h +1 -13
- data/ext/libcouchbase/include/libcouchbase/plugins/io/bsdio-inl.c +1 -1
- data/ext/libcouchbase/include/libcouchbase/subdoc.h +0 -9
- data/ext/libcouchbase/include/libcouchbase/views.h +1 -7
- data/ext/libcouchbase/include/libcouchbase/visibility.h +0 -1
- data/ext/libcouchbase/include/memcached/protocol_binary.h +1131 -29
- data/ext/libcouchbase/include/memcached/vbucket.h +42 -0
- data/ext/libcouchbase/packaging/parse-git-describe.pl +1 -1
- data/ext/libcouchbase/plugins/io/libev/libev_io_opts.h +2 -3
- data/ext/libcouchbase/src/README.md +2 -0
- data/ext/libcouchbase/src/auth-priv.h +0 -1
- data/ext/libcouchbase/src/auth.cc +4 -10
- data/ext/libcouchbase/src/bootstrap.c +269 -0
- data/ext/libcouchbase/src/bootstrap.h +39 -50
- data/ext/libcouchbase/src/bucketconfig/bc_cccp.cc +117 -84
- data/ext/libcouchbase/src/bucketconfig/bc_file.c +347 -0
- data/ext/libcouchbase/src/bucketconfig/bc_http.c +630 -0
- data/ext/libcouchbase/src/bucketconfig/bc_http.h +25 -50
- data/ext/libcouchbase/src/bucketconfig/bc_mcraw.c +150 -0
- data/ext/libcouchbase/src/bucketconfig/clconfig.h +386 -407
- data/ext/libcouchbase/src/bucketconfig/confmon.c +474 -0
- data/ext/libcouchbase/src/cbft.cc +27 -22
- data/ext/libcouchbase/src/cntl.cc +19 -30
- data/ext/libcouchbase/src/connspec.cc +1 -48
- data/ext/libcouchbase/src/connspec.h +0 -27
- data/ext/libcouchbase/src/dump.cc +2 -2
- data/ext/libcouchbase/src/getconfig.cc +33 -7
- data/ext/libcouchbase/src/handler.cc +2 -0
- data/ext/libcouchbase/src/hostlist.cc +36 -0
- data/ext/libcouchbase/src/hostlist.h +62 -41
- data/ext/libcouchbase/src/http/http-priv.h +112 -125
- data/ext/libcouchbase/src/http/http.cc +30 -15
- data/ext/libcouchbase/src/http/http.h +34 -1
- data/ext/libcouchbase/src/http/http_io.cc +26 -22
- data/ext/libcouchbase/src/instance.cc +23 -94
- data/ext/libcouchbase/src/internal.h +26 -52
- data/ext/libcouchbase/src/jsparse/parser.cc +202 -146
- data/ext/libcouchbase/src/jsparse/parser.h +98 -91
- data/ext/libcouchbase/src/lcbht/lcbht.c +282 -0
- data/ext/libcouchbase/src/lcbht/lcbht.h +163 -174
- data/ext/libcouchbase/src/lcbio/connect.c +557 -0
- data/ext/libcouchbase/src/lcbio/connect.h +2 -9
- data/ext/libcouchbase/src/lcbio/ctx.c +1 -1
- data/ext/libcouchbase/src/lcbio/iotable.h +16 -61
- data/ext/libcouchbase/src/lcbio/ioutils.h +1 -1
- data/ext/libcouchbase/src/lcbio/manager.c +2 -2
- data/ext/libcouchbase/src/mc/mcreq.h +2 -9
- data/ext/libcouchbase/src/mcserver/mcserver.cc +34 -143
- data/ext/libcouchbase/src/mcserver/mcserver.h +12 -7
- data/ext/libcouchbase/src/mcserver/negotiate.cc +38 -132
- data/ext/libcouchbase/src/n1ql/ixmgmt.cc +2 -1
- data/ext/libcouchbase/src/n1ql/n1ql.cc +32 -56
- data/ext/libcouchbase/src/newconfig.cc +6 -6
- data/ext/libcouchbase/src/nodeinfo.cc +2 -2
- data/ext/libcouchbase/src/operations/{cbflush.cc → cbflush.c} +15 -7
- data/ext/libcouchbase/src/operations/{counter.cc → counter.c} +0 -0
- data/ext/libcouchbase/src/operations/durability.cc +26 -6
- data/ext/libcouchbase/src/operations/durability_internal.h +3 -6
- data/ext/libcouchbase/src/operations/{get.cc → get.c} +26 -24
- data/ext/libcouchbase/src/operations/{observe.cc → observe.c} +93 -68
- data/ext/libcouchbase/src/operations/{pktfwd.cc → pktfwd.c} +0 -0
- data/ext/libcouchbase/src/operations/{remove.cc → remove.c} +0 -0
- data/ext/libcouchbase/src/operations/stats.cc +8 -3
- data/ext/libcouchbase/src/operations/{store.cc → store.c} +32 -27
- data/ext/libcouchbase/src/operations/subdoc.cc +18 -38
- data/ext/libcouchbase/src/operations/{touch.cc → touch.c} +0 -0
- data/ext/libcouchbase/src/packetutils.c +37 -0
- data/ext/libcouchbase/src/packetutils.h +2 -2
- data/ext/libcouchbase/src/probes.d +1 -1
- data/ext/libcouchbase/src/{retrychk.cc → retrychk.c} +3 -2
- data/ext/libcouchbase/src/retryq.cc +4 -4
- data/ext/libcouchbase/src/settings.c +0 -3
- data/ext/libcouchbase/src/settings.h +0 -5
- data/ext/libcouchbase/src/simplestring.c +211 -0
- data/ext/libcouchbase/src/simplestring.h +228 -0
- data/ext/libcouchbase/src/ssl/ssl_c.c +0 -1
- data/ext/libcouchbase/src/ssl/ssl_common.c +0 -2
- data/ext/libcouchbase/src/ssl/ssl_e.c +1 -0
- data/ext/libcouchbase/src/ssobuf.h +82 -0
- data/ext/libcouchbase/src/trace.h +4 -4
- data/ext/libcouchbase/src/vbucket/vbucket.c +1 -0
- data/ext/libcouchbase/src/views/{docreq.cc → docreq.c} +54 -48
- data/ext/libcouchbase/src/views/docreq.h +30 -24
- data/ext/libcouchbase/src/views/viewreq.c +358 -0
- data/ext/libcouchbase/src/views/viewreq.h +13 -43
- data/ext/libcouchbase/tests/basic/t_connstr.cc +50 -89
- data/ext/libcouchbase/tests/basic/t_host.cc +75 -67
- data/ext/libcouchbase/tests/basic/t_jsparse.cc +78 -27
- data/ext/libcouchbase/tests/basic/t_string.cc +112 -0
- data/ext/libcouchbase/tests/htparse/t_basic.cc +78 -58
- data/ext/libcouchbase/tests/iotests/mock-environment.h +1 -2
- data/ext/libcouchbase/tests/iotests/t_confmon.cc +114 -96
- data/ext/libcouchbase/tests/mc/t_alloc.cc +9 -9
- data/ext/libcouchbase/tools/cbc-pillowfight.cc +1 -1
- data/lib/libcouchbase/ext/tasks.rb +6 -2
- data/lib/libcouchbase/query_view.rb +1 -1
- data/lib/libcouchbase/results_fiber.rb +6 -6
- data/lib/libcouchbase/version.rb +1 -1
- metadata +26 -26
- data/ext/libcouchbase/src/bootstrap.cc +0 -216
- data/ext/libcouchbase/src/bucketconfig/bc_file.cc +0 -281
- data/ext/libcouchbase/src/bucketconfig/bc_http.cc +0 -528
- data/ext/libcouchbase/src/bucketconfig/bc_mcraw.cc +0 -115
- data/ext/libcouchbase/src/bucketconfig/confmon.cc +0 -378
- data/ext/libcouchbase/src/dns-srv.cc +0 -142
- data/ext/libcouchbase/src/errmap.cc +0 -107
- data/ext/libcouchbase/src/errmap.h +0 -113
- data/ext/libcouchbase/src/lcbht/lcbht.cc +0 -177
- data/ext/libcouchbase/src/lcbio/connect.cc +0 -562
- data/ext/libcouchbase/src/lcbio/timer-cxx.h +0 -87
- data/ext/libcouchbase/src/mctx-helper.h +0 -51
- data/ext/libcouchbase/src/views/viewreq.cc +0 -318
- data/ext/libcouchbase/tests/iotests/t_errmap.cc +0 -97
|
@@ -20,111 +20,60 @@
|
|
|
20
20
|
#include <libcouchbase/couchbase.h>
|
|
21
21
|
#include <libcouchbase/views.h>
|
|
22
22
|
#include "contrib/jsonsl/jsonsl.h"
|
|
23
|
-
#include "
|
|
24
|
-
#include <string>
|
|
23
|
+
#include "simplestring.h"
|
|
25
24
|
|
|
26
|
-
|
|
27
|
-
|
|
25
|
+
#ifdef __cplusplus
|
|
26
|
+
extern "C" {
|
|
27
|
+
#endif
|
|
28
28
|
|
|
29
|
-
struct
|
|
29
|
+
typedef struct lcbvrow_PARSER_st lcbjsp_PARSER;
|
|
30
30
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
lcb_IOV geo;
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
struct Parser {
|
|
40
|
-
enum Mode {
|
|
41
|
-
MODE_VIEWS,
|
|
42
|
-
MODE_N1QL,
|
|
43
|
-
MODE_FTS
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
struct Actions {
|
|
47
|
-
/**
|
|
48
|
-
* Called when a row is received.
|
|
49
|
-
* This is a row of view data. You can parse this as JSON from your
|
|
50
|
-
* favorite decoder/converter
|
|
51
|
-
*/
|
|
52
|
-
virtual void JSPARSE_on_row(const Row&) = 0;
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* A JSON parse error occured. The payload will contain string data. This
|
|
56
|
-
* may be JSON (but this is not likely).
|
|
57
|
-
* The callback will be delivered twice. First when the error is noticed,
|
|
58
|
-
* and second at the end (instead of a COMPLETE callback)
|
|
59
|
-
*/
|
|
60
|
-
virtual void JSPARSE_on_error(const std::string& buf) = 0;
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* All the rows have been returned. In this case, the data is the 'meta'.
|
|
64
|
-
* This is a valid JSON payload which was returned from the server.
|
|
65
|
-
* The "rows" : [] array will be empty.
|
|
66
|
-
*/
|
|
67
|
-
virtual void JSPARSE_on_complete(const std::string& meta) = 0;
|
|
68
|
-
|
|
69
|
-
virtual ~Actions(){}
|
|
70
|
-
};
|
|
31
|
+
typedef enum {
|
|
32
|
+
LCBJSP_MODE_VIEWS,
|
|
33
|
+
LCBJSP_MODE_N1QL,
|
|
34
|
+
LCBJSP_MODE_FTS
|
|
35
|
+
} lcbjsp_MODE;
|
|
71
36
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
*
|
|
75
|
-
|
|
76
|
-
* in chunks and callbacks will be invoked as each row is read.
|
|
77
|
-
*/
|
|
78
|
-
Parser(Mode mode, Actions* actions_);
|
|
79
|
-
~Parser();
|
|
37
|
+
typedef enum {
|
|
38
|
+
/**This is a row of view data. You can parse this as JSON from your
|
|
39
|
+
* favorite decoder/converter */
|
|
40
|
+
LCBJSP_TYPE_ROW,
|
|
80
41
|
|
|
81
42
|
/**
|
|
82
|
-
*
|
|
83
|
-
* This
|
|
84
|
-
*
|
|
43
|
+
* All the rows have been returned. In this case, the data is the 'meta'.
|
|
44
|
+
* This is a valid JSON payload which was returned from the server.
|
|
45
|
+
* The "rows" : [] array will be empty.
|
|
85
46
|
*/
|
|
86
|
-
|
|
47
|
+
LCBJSP_TYPE_COMPLETE,
|
|
87
48
|
|
|
88
49
|
/**
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
* be
|
|
50
|
+
* A JSON parse error occured. The payload will contain string data. This
|
|
51
|
+
* may be JSON (but this is not likely).
|
|
52
|
+
* The callback will be delivered twice. First when the error is noticed,
|
|
53
|
+
* and second at the end (instead of a COMPLETE callback)
|
|
92
54
|
*/
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
feed(s.c_str(), s.size());
|
|
96
|
-
}
|
|
55
|
+
LCBJSP_TYPE_ERROR
|
|
56
|
+
} lcbjsp_ROWTYPE;
|
|
97
57
|
|
|
98
|
-
/**
|
|
99
|
-
* Parse the row buffer into its constituent parts. This should be called
|
|
100
|
-
* if you want to split the row into its basic 'docid', 'key' and 'value'
|
|
101
|
-
* fields
|
|
102
|
-
* @param vp The parser to use
|
|
103
|
-
* @param vr The row to parse. This assumes the row's "row" field is properly
|
|
104
|
-
* set.
|
|
105
|
-
*/
|
|
106
|
-
void parse_viewrow(Row& vr);
|
|
107
58
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
void get_postmortem(lcb_IOV& out) const;
|
|
59
|
+
typedef struct {
|
|
60
|
+
lcbjsp_ROWTYPE type; /**< The type of data encapsulated */
|
|
61
|
+
lcb_IOV docid;
|
|
62
|
+
lcb_IOV key;
|
|
63
|
+
lcb_IOV value;
|
|
64
|
+
lcb_IOV row;
|
|
65
|
+
lcb_IOV geo;
|
|
66
|
+
} lcbjsp_ROW;
|
|
117
67
|
|
|
118
|
-
|
|
119
|
-
inline void combine_meta();
|
|
120
|
-
inline static const char *jprstr_for_mode(Mode);
|
|
68
|
+
typedef void (*lcbjsp_CALLBACK)(lcbjsp_PARSER*,const lcbjsp_ROW*);
|
|
121
69
|
|
|
70
|
+
struct lcbvrow_PARSER_st {
|
|
122
71
|
jsonsl_t jsn; /**< Parser for the row itself */
|
|
123
72
|
jsonsl_t jsn_rdetails; /**< Parser for the row details */
|
|
124
73
|
jsonsl_jpr_t jpr; /**< jsonpointer match object */
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
74
|
+
lcb_string meta_buf; /**< String containing the skeleton (outer layer) */
|
|
75
|
+
lcb_string current_buf; /**< Scratch/read buffer */
|
|
76
|
+
lcb_string last_hk; /**< Last hashkey */
|
|
128
77
|
|
|
129
78
|
lcb_U8 mode;
|
|
130
79
|
|
|
@@ -152,15 +101,73 @@ struct Parser {
|
|
|
152
101
|
*/
|
|
153
102
|
size_t last_row_endpos;
|
|
154
103
|
|
|
104
|
+
void *data;
|
|
105
|
+
|
|
155
106
|
/**
|
|
156
107
|
* std::string to contain parsed document ID.
|
|
157
108
|
*/
|
|
158
|
-
|
|
109
|
+
void *cxx_data;
|
|
159
110
|
|
|
160
111
|
/* callback to invoke */
|
|
161
|
-
|
|
112
|
+
lcbjsp_CALLBACK callback;
|
|
162
113
|
};
|
|
163
114
|
|
|
115
|
+
/**
|
|
116
|
+
* Creates a new vrow context object.
|
|
117
|
+
* You must set callbacks on this object if you wish it to be useful.
|
|
118
|
+
* You must feed it data (calling vrow_feed) as well. The data may be fed
|
|
119
|
+
* in chunks and callbacks will be invoked as each row is read.
|
|
120
|
+
*/
|
|
121
|
+
lcbjsp_PARSER*
|
|
122
|
+
lcbjsp_create(int);
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Resets the context to a pristine state. Callbacks and cookies are kept.
|
|
126
|
+
* This may be more efficient than allocating/freeing a context each time
|
|
127
|
+
* (as this can be expensive with the jsonsl structures)
|
|
128
|
+
*/
|
|
129
|
+
void
|
|
130
|
+
lcbjsp_reset(lcbjsp_PARSER *ctx);
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Frees a vrow object created by vrow_create
|
|
134
|
+
*/
|
|
135
|
+
void
|
|
136
|
+
lcbjsp_free(lcbjsp_PARSER *ctx);
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Feeds data into the vrow. The callback may be invoked multiple times
|
|
140
|
+
* in this function. In the context of normal lcb usage, this will typically
|
|
141
|
+
* be invoked from within an http_data_callback.
|
|
142
|
+
*/
|
|
143
|
+
void
|
|
144
|
+
lcbjsp_feed(lcbjsp_PARSER *ctx, const char *data, size_t ndata);
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Parse the row buffer into its constituent parts. This should be called
|
|
148
|
+
* if you want to split the row into its basic 'docid', 'key' and 'value'
|
|
149
|
+
* fields
|
|
150
|
+
* @param vp The parser to use
|
|
151
|
+
* @param vr The row to parse. This assumes the row's "row" field is properly
|
|
152
|
+
* set.
|
|
153
|
+
*/
|
|
154
|
+
void
|
|
155
|
+
lcbjsp_parse_viewrow(lcbjsp_PARSER *vp, lcbjsp_ROW *vr);
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Get the raw contents of the current buffer. This can be used to debug errors.
|
|
159
|
+
*
|
|
160
|
+
* Note that the buffer may be partial or malformed or otherwise unsuitable
|
|
161
|
+
* for structured inspection, but may help human observers debug problems.
|
|
162
|
+
*
|
|
163
|
+
* @param v The parser
|
|
164
|
+
* @param out The iov structure to contain the buffer/offset
|
|
165
|
+
*/
|
|
166
|
+
void
|
|
167
|
+
lcbjsp_get_postmortem(const lcbjsp_PARSER *v, lcb_IOV *out);
|
|
168
|
+
|
|
169
|
+
#ifdef __cplusplus
|
|
164
170
|
}
|
|
165
|
-
|
|
171
|
+
#endif
|
|
172
|
+
|
|
166
173
|
#endif /* LCB_VIEWROW_H_ */
|
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
|
|
2
|
+
/*
|
|
3
|
+
* Copyright 2014 Couchbase, Inc.
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
#include "lcbht.h"
|
|
19
|
+
#include "sllist-inl.h"
|
|
20
|
+
#include "contrib/http_parser/http_parser.h"
|
|
21
|
+
#include "settings.h"
|
|
22
|
+
enum last_call_type {
|
|
23
|
+
CB_NONE,
|
|
24
|
+
CB_HDR_KEY,
|
|
25
|
+
CB_HDR_VALUE,
|
|
26
|
+
CB_HDR_DONE,
|
|
27
|
+
CB_BODY,
|
|
28
|
+
CB_MSG_DONE
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
struct lcbht_PARSER {
|
|
32
|
+
http_parser parser;
|
|
33
|
+
lcbht_RESPONSE resp; /**< Current response being processed */
|
|
34
|
+
lcb_settings *settings;
|
|
35
|
+
enum last_call_type lastcall;
|
|
36
|
+
|
|
37
|
+
/* this stuff for parse_ex() */
|
|
38
|
+
const char *last_body;
|
|
39
|
+
unsigned last_bodylen;
|
|
40
|
+
char paused;
|
|
41
|
+
char is_ex;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
static int
|
|
45
|
+
on_hdr_key(http_parser *pb, const char *s, size_t n)
|
|
46
|
+
{
|
|
47
|
+
lcbht_pPARSER p = (void *)pb;
|
|
48
|
+
lcbht_MIMEHDR *hdr;
|
|
49
|
+
lcbht_RESPONSE *resp = &p->resp;
|
|
50
|
+
|
|
51
|
+
if (p->lastcall != CB_HDR_KEY) {
|
|
52
|
+
/* new key */
|
|
53
|
+
hdr = calloc(1, sizeof *hdr);
|
|
54
|
+
sllist_append(&resp->headers, &hdr->slnode);
|
|
55
|
+
lcb_string_init(&hdr->buf_);
|
|
56
|
+
} else {
|
|
57
|
+
hdr = SLLIST_ITEM(resp->headers.last, lcbht_MIMEHDR, slnode);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/* append key data */
|
|
61
|
+
lcb_string_append(&hdr->buf_, s, n);
|
|
62
|
+
p->lastcall = CB_HDR_KEY;
|
|
63
|
+
return 0;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
static int
|
|
67
|
+
on_hdr_value(http_parser *pb, const char *s, size_t n)
|
|
68
|
+
{
|
|
69
|
+
lcbht_pPARSER p = (void *)pb;
|
|
70
|
+
lcbht_MIMEHDR *hdr;
|
|
71
|
+
lcbht_RESPONSE *resp = &p->resp;
|
|
72
|
+
|
|
73
|
+
hdr = SLLIST_ITEM(resp->headers.last, lcbht_MIMEHDR, slnode);
|
|
74
|
+
if (p->lastcall == CB_HDR_KEY) {
|
|
75
|
+
lcb_string_appendz(&hdr->buf_, ":");
|
|
76
|
+
}
|
|
77
|
+
lcb_string_append(&hdr->buf_, s, n);
|
|
78
|
+
p->lastcall = CB_HDR_VALUE;
|
|
79
|
+
return 0;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
static int
|
|
83
|
+
on_hdr_done(http_parser *pb)
|
|
84
|
+
{
|
|
85
|
+
lcbht_pPARSER p = (void *)pb;
|
|
86
|
+
lcbht_RESPONSE *resp = &p->resp;
|
|
87
|
+
sllist_iterator iter;
|
|
88
|
+
resp->state |= LCBHT_S_HTSTATUS|LCBHT_S_HEADER;
|
|
89
|
+
|
|
90
|
+
/* extract the status */
|
|
91
|
+
resp->status = pb->status_code;
|
|
92
|
+
p->lastcall = CB_HDR_DONE;
|
|
93
|
+
|
|
94
|
+
/** Iterate through all the headers and do proper formatting */
|
|
95
|
+
SLLIST_ITERFOR(&resp->headers, &iter) {
|
|
96
|
+
char *delim;
|
|
97
|
+
lcbht_MIMEHDR *hdr = SLLIST_ITEM(iter.cur, lcbht_MIMEHDR, slnode);
|
|
98
|
+
|
|
99
|
+
delim = strstr(hdr->buf_.base, ":");
|
|
100
|
+
hdr->key = hdr->buf_.base;
|
|
101
|
+
|
|
102
|
+
if (delim) {
|
|
103
|
+
*delim = '\0';
|
|
104
|
+
hdr->value = delim + 1;
|
|
105
|
+
} else {
|
|
106
|
+
hdr->value = "";
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return 0;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
static int
|
|
113
|
+
on_body(http_parser *pb, const char *s, size_t n)
|
|
114
|
+
{
|
|
115
|
+
lcbht_pPARSER p = (void *)pb;
|
|
116
|
+
lcbht_RESPONSE *resp = &p->resp;
|
|
117
|
+
if (p->is_ex) {
|
|
118
|
+
p->last_body = s;
|
|
119
|
+
p->last_bodylen = n;
|
|
120
|
+
p->paused = 1;
|
|
121
|
+
_lcb_http_parser_pause(pb, 1);
|
|
122
|
+
} else {
|
|
123
|
+
lcb_string_append(&resp->body, s, n);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
p->lastcall = CB_BODY;
|
|
127
|
+
resp->state |= LCBHT_S_BODY;
|
|
128
|
+
return 0;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
static int
|
|
132
|
+
on_msg_done(http_parser *pb)
|
|
133
|
+
{
|
|
134
|
+
lcbht_pPARSER p = (void *)pb;
|
|
135
|
+
lcbht_RESPONSE *resp = &p->resp;
|
|
136
|
+
resp->state |= LCBHT_S_DONE;
|
|
137
|
+
return 0;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
static struct http_parser_settings Parser_Settings = {
|
|
142
|
+
NULL, /* msg_begin */
|
|
143
|
+
NULL, /* on_url */
|
|
144
|
+
on_hdr_key,
|
|
145
|
+
on_hdr_value,
|
|
146
|
+
on_hdr_done,
|
|
147
|
+
on_body,
|
|
148
|
+
on_msg_done
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
lcbht_pPARSER
|
|
152
|
+
lcbht_new(lcb_settings *settings)
|
|
153
|
+
{
|
|
154
|
+
lcbht_pPARSER ret = calloc(1, sizeof *ret);
|
|
155
|
+
lcbht_reset(ret);
|
|
156
|
+
ret->settings = settings;
|
|
157
|
+
lcb_settings_ref(settings);
|
|
158
|
+
return ret;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
void
|
|
162
|
+
lcbht_free(lcbht_pPARSER parser)
|
|
163
|
+
{
|
|
164
|
+
lcbht_clear_response(&parser->resp);
|
|
165
|
+
lcb_settings_unref(parser->settings);
|
|
166
|
+
free(parser);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
lcbht_RESPSTATE
|
|
170
|
+
lcbht_parse(lcbht_pPARSER parser, const void *data, unsigned ndata)
|
|
171
|
+
{
|
|
172
|
+
size_t nb;
|
|
173
|
+
parser->is_ex = 0;
|
|
174
|
+
nb = _lcb_http_parser_execute(&parser->parser, &Parser_Settings, data, ndata);
|
|
175
|
+
if (nb != ndata) {
|
|
176
|
+
parser->resp.state |= LCBHT_S_ERROR;
|
|
177
|
+
}
|
|
178
|
+
return parser->resp.state;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
lcbht_RESPSTATE
|
|
182
|
+
lcbht_parse_ex(lcbht_pPARSER parser, const void *data, unsigned ndata,
|
|
183
|
+
unsigned *nused, unsigned *nbody, const char **pbody)
|
|
184
|
+
{
|
|
185
|
+
size_t nb;
|
|
186
|
+
parser->is_ex = 1;
|
|
187
|
+
nb = _lcb_http_parser_execute(&parser->parser, &Parser_Settings, data, ndata);
|
|
188
|
+
if (nb != ndata) {
|
|
189
|
+
if (parser->paused) {
|
|
190
|
+
_lcb_http_parser_pause(&parser->parser, 0);
|
|
191
|
+
parser->paused = 0;
|
|
192
|
+
} else {
|
|
193
|
+
parser->resp.state |= LCBHT_S_ERROR;
|
|
194
|
+
return parser->resp.state;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
*nused = nb;
|
|
199
|
+
*nbody = parser->last_bodylen;
|
|
200
|
+
*pbody = parser->last_body;
|
|
201
|
+
|
|
202
|
+
parser->last_body = NULL;
|
|
203
|
+
parser->last_bodylen = 0;
|
|
204
|
+
return parser->resp.state;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
lcbht_RESPONSE *
|
|
208
|
+
lcbht_get_response(lcbht_pPARSER parser)
|
|
209
|
+
{
|
|
210
|
+
return &parser->resp;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
int
|
|
214
|
+
lcbht_can_keepalive(lcbht_pPARSER parser)
|
|
215
|
+
{
|
|
216
|
+
if (!(parser->resp.state & LCBHT_S_DONE)) {
|
|
217
|
+
return 0;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
if (parser->resp.state & LCBHT_S_ERROR) {
|
|
221
|
+
return 0;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
return _lcb_http_should_keep_alive(&parser->parser);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
void
|
|
228
|
+
lcbht_clear_response(lcbht_RESPONSE *resp)
|
|
229
|
+
{
|
|
230
|
+
sllist_iterator iter;
|
|
231
|
+
SLLIST_ITERFOR(&resp->headers, &iter) {
|
|
232
|
+
lcbht_MIMEHDR *hdr = SLLIST_ITEM(iter.cur, lcbht_MIMEHDR, slnode);
|
|
233
|
+
lcb_string_release(&hdr->buf_);
|
|
234
|
+
sllist_iter_remove(&resp->headers, &iter);
|
|
235
|
+
free(hdr);
|
|
236
|
+
}
|
|
237
|
+
lcb_string_release(&resp->body);
|
|
238
|
+
resp->state = 0;
|
|
239
|
+
resp->status = 0;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
void
|
|
243
|
+
lcbht_reset(lcbht_pPARSER parser)
|
|
244
|
+
{
|
|
245
|
+
lcbht_clear_response(&parser->resp);
|
|
246
|
+
_lcb_http_parser_init(&parser->parser, HTTP_RESPONSE);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
const char *
|
|
250
|
+
lcbht_get_resphdr(const lcbht_RESPONSE *resp, const char *key)
|
|
251
|
+
{
|
|
252
|
+
sllist_node *curnode;
|
|
253
|
+
SLLIST_ITERBASIC(&resp->headers, curnode) {
|
|
254
|
+
lcbht_MIMEHDR *hdr = SLLIST_ITEM(curnode, lcbht_MIMEHDR, slnode);
|
|
255
|
+
if (!strcmp(hdr->key, key)) {
|
|
256
|
+
return hdr->value;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
return NULL;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
char **
|
|
263
|
+
lcbht_make_resphdrlist(lcbht_RESPONSE *response)
|
|
264
|
+
{
|
|
265
|
+
char **ret;
|
|
266
|
+
unsigned nhdrs = 0;
|
|
267
|
+
unsigned curix = 0;
|
|
268
|
+
sllist_node *curnode;
|
|
269
|
+
SLLIST_ITERBASIC(&response->headers, curnode) {
|
|
270
|
+
nhdrs++;
|
|
271
|
+
}
|
|
272
|
+
ret = malloc(sizeof(*ret) * (nhdrs + 1) * 2);
|
|
273
|
+
|
|
274
|
+
SLLIST_ITERBASIC(&response->headers, curnode) {
|
|
275
|
+
lcbht_MIMEHDR *hdr = SLLIST_ITEM(curnode, lcbht_MIMEHDR, slnode);
|
|
276
|
+
ret[curix++] = strdup(hdr->key);
|
|
277
|
+
ret[curix++] = strdup(hdr->value);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
ret[curix] = NULL;
|
|
281
|
+
return ret;
|
|
282
|
+
}
|