amqp_consumer.c

filter: Statementstmt 0.0% 0 / 149 Branchbran 0.0% 0 / 114 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 263 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Net/AMQP/RabbitMQ.pm ↑ Summary amqp_hostcheck.c »
1 /* vim:set ft=c ts=2 sw=2 sts=2 et cindent: */
2 /*
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MIT
5 *
6 * Portions created by Alan Antonuk are Copyright (c) 2013-2014
7 * Alan Antonuk. All Rights Reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person
10 * obtaining a copy of this software and associated documentation
11 * files (the "Software"), to deal in the Software without
12 * restriction, including without limitation the rights to use, copy,
13 * modify, merge, publish, distribute, sublicense, and/or sell copies
14 * of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * SOFTWARE.
28 * ***** END LICENSE BLOCK *****
29 */
30 #include "amqp.h"
31 #include "amqp_private.h"
32 #include "amqp_socket.h"
33  
34 #include <stdlib.h>
35 #include <string.h>
36  
37 static
38 0 int amqp_basic_properties_clone(amqp_basic_properties_t *original,
39 amqp_basic_properties_t *clone,
40 amqp_pool_t *pool)
41 {
42 0 memset(clone, 0, sizeof(amqp_basic_properties_t));
43 0 clone->_flags = original->_flags;
44  
45 #define CLONE_BYTES_POOL(original, clone, pool) \
46 if (0 == original.len) { \
47 clone = amqp_empty_bytes; \
48 } else { \
49 amqp_pool_alloc_bytes(pool, original.len, &clone); \
50 if (NULL == clone.bytes) { \
51 return AMQP_STATUS_NO_MEMORY; \
52 } \
53 memcpy(clone.bytes, original.bytes, clone.len); \
54 }
55  
56 0 ▶ if (clone->_flags & AMQP_BASIC_CONTENT_TYPE_FLAG) {
Branch: 1 branch
BranchTrueFalseTotal
if (clone->_flags & AMQP_BASIC_CONTENT_TYPE_FLAG) { 0 0 0
57 0 ▶ CLONE_BYTES_POOL(original->content_type, clone->content_type, pool)
Branch: 2 branches
BranchTrueFalseTotal
CLONE_BYTES_POOL(original->content_type, clone->content_type, pool) 0 0 0
CLONE_BYTES_POOL(original->content_type, clone->content_type, pool) 0 0 0
58 }
59  
60 0 ▶ if (clone->_flags & AMQP_BASIC_CONTENT_ENCODING_FLAG) {
Branch: 1 branch
BranchTrueFalseTotal
if (clone->_flags & AMQP_BASIC_CONTENT_ENCODING_FLAG) { 0 0 0
61 0 ▶ CLONE_BYTES_POOL(original->content_encoding, clone->content_encoding, pool)
Branch: 2 branches
BranchTrueFalseTotal
CLONE_BYTES_POOL(original->content_encoding, clone->content_encoding, pool) 0 0 0
CLONE_BYTES_POOL(original->content_encoding, clone->content_encoding, pool) 0 0 0
62 }
63  
64 0 ▶ if (clone->_flags & AMQP_BASIC_HEADERS_FLAG) {
Branch: 1 branch
BranchTrueFalseTotal
if (clone->_flags & AMQP_BASIC_HEADERS_FLAG) { 0 0 0
65 0 int res = amqp_table_clone(&original->headers, &clone->headers, pool);
66 0 ▶ if (AMQP_STATUS_OK != res) {
Branch: 1 branch
BranchTrueFalseTotal
if (AMQP_STATUS_OK != res) { 0 0 0
67 0 return res;
68 }
69 }
70  
71 0 ▶ if (clone->_flags & AMQP_BASIC_DELIVERY_MODE_FLAG) {
Branch: 1 branch
BranchTrueFalseTotal
if (clone->_flags & AMQP_BASIC_DELIVERY_MODE_FLAG) { 0 0 0
72 0 clone->delivery_mode = original->delivery_mode;
73 }
74  
75 0 ▶ if (clone->_flags & AMQP_BASIC_PRIORITY_FLAG) {
Branch: 1 branch
BranchTrueFalseTotal
if (clone->_flags & AMQP_BASIC_PRIORITY_FLAG) { 0 0 0
76 0 clone->priority = original->priority;
77 }
78  
79 0 ▶ if (clone->_flags & AMQP_BASIC_CORRELATION_ID_FLAG) {
Branch: 1 branch
BranchTrueFalseTotal
if (clone->_flags & AMQP_BASIC_CORRELATION_ID_FLAG) { 0 0 0
80 0 ▶ CLONE_BYTES_POOL(original->correlation_id, clone->correlation_id, pool)
Branch: 2 branches
BranchTrueFalseTotal
CLONE_BYTES_POOL(original->correlation_id, clone->correlation_id, pool) 0 0 0
CLONE_BYTES_POOL(original->correlation_id, clone->correlation_id, pool) 0 0 0
81 }
82  
83 0 ▶ if (clone->_flags & AMQP_BASIC_REPLY_TO_FLAG) {
Branch: 1 branch
BranchTrueFalseTotal
if (clone->_flags & AMQP_BASIC_REPLY_TO_FLAG) { 0 0 0
84 0 ▶ CLONE_BYTES_POOL(original->reply_to, clone->reply_to, pool)
Branch: 2 branches
BranchTrueFalseTotal
CLONE_BYTES_POOL(original->reply_to, clone->reply_to, pool) 0 0 0
CLONE_BYTES_POOL(original->reply_to, clone->reply_to, pool) 0 0 0
85 }
86  
87 0 ▶ if (clone->_flags & AMQP_BASIC_EXPIRATION_FLAG) {
Branch: 1 branch
BranchTrueFalseTotal
if (clone->_flags & AMQP_BASIC_EXPIRATION_FLAG) { 0 0 0
88 0 ▶ CLONE_BYTES_POOL(original->expiration, clone->expiration, pool)
Branch: 2 branches
BranchTrueFalseTotal
CLONE_BYTES_POOL(original->expiration, clone->expiration, pool) 0 0 0
CLONE_BYTES_POOL(original->expiration, clone->expiration, pool) 0 0 0
89 }
90  
91 0 ▶ if (clone->_flags & AMQP_BASIC_MESSAGE_ID_FLAG) {
Branch: 1 branch
BranchTrueFalseTotal
if (clone->_flags & AMQP_BASIC_MESSAGE_ID_FLAG) { 0 0 0
92 0 ▶ CLONE_BYTES_POOL(original->message_id, clone->message_id, pool)
Branch: 2 branches
BranchTrueFalseTotal
CLONE_BYTES_POOL(original->message_id, clone->message_id, pool) 0 0 0
CLONE_BYTES_POOL(original->message_id, clone->message_id, pool) 0 0 0
93 }
94  
95 0 ▶ if (clone->_flags & AMQP_BASIC_TIMESTAMP_FLAG) {
Branch: 1 branch
BranchTrueFalseTotal
if (clone->_flags & AMQP_BASIC_TIMESTAMP_FLAG) { 0 0 0
96 0 clone->timestamp = original->timestamp;
97 }
98  
99 0 ▶ if (clone->_flags & AMQP_BASIC_TYPE_FLAG) {
Branch: 1 branch
BranchTrueFalseTotal
if (clone->_flags & AMQP_BASIC_TYPE_FLAG) { 0 0 0
100 0 ▶ CLONE_BYTES_POOL(original->type, clone->type, pool)
Branch: 2 branches
BranchTrueFalseTotal
CLONE_BYTES_POOL(original->type, clone->type, pool) 0 0 0
CLONE_BYTES_POOL(original->type, clone->type, pool) 0 0 0
101 }
102  
103 0 ▶ if (clone->_flags & AMQP_BASIC_USER_ID_FLAG) {
Branch: 1 branch
BranchTrueFalseTotal
if (clone->_flags & AMQP_BASIC_USER_ID_FLAG) { 0 0 0
104 0 ▶ CLONE_BYTES_POOL(original->user_id, clone->user_id, pool)
Branch: 2 branches
BranchTrueFalseTotal
CLONE_BYTES_POOL(original->user_id, clone->user_id, pool) 0 0 0
CLONE_BYTES_POOL(original->user_id, clone->user_id, pool) 0 0 0
105 }
106  
107 0 ▶ if (clone->_flags & AMQP_BASIC_APP_ID_FLAG) {
Branch: 1 branch
BranchTrueFalseTotal
if (clone->_flags & AMQP_BASIC_APP_ID_FLAG) { 0 0 0
108 0 ▶ CLONE_BYTES_POOL(original->app_id, clone->app_id, pool)
Branch: 2 branches
BranchTrueFalseTotal
CLONE_BYTES_POOL(original->app_id, clone->app_id, pool) 0 0 0
CLONE_BYTES_POOL(original->app_id, clone->app_id, pool) 0 0 0
109 }
110  
111 0 ▶ if (clone->_flags & AMQP_BASIC_CLUSTER_ID_FLAG) {
Branch: 1 branch
BranchTrueFalseTotal
if (clone->_flags & AMQP_BASIC_CLUSTER_ID_FLAG) { 0 0 0
112 0 ▶ CLONE_BYTES_POOL(original->cluster_id, clone->cluster_id, pool)
Branch: 2 branches
BranchTrueFalseTotal
CLONE_BYTES_POOL(original->cluster_id, clone->cluster_id, pool) 0 0 0
CLONE_BYTES_POOL(original->cluster_id, clone->cluster_id, pool) 0 0 0
113 }
114  
115 0 return AMQP_STATUS_OK;
116 #undef CLONE_BYTES_POOL
117 }
118  
119  
120 0 void amqp_destroy_message(amqp_message_t *message)
121 {
122 0 empty_amqp_pool(&message->pool);
123 0 amqp_bytes_free(message->body);
124 0 }
125  
126 0 void amqp_destroy_envelope(amqp_envelope_t *envelope)
127 {
128 0 amqp_destroy_message(&envelope->message);
129 0 amqp_bytes_free(envelope->routing_key);
130 0 amqp_bytes_free(envelope->exchange);
131 0 amqp_bytes_free(envelope->consumer_tag);
132 0 }
133  
134 static
135 0 int amqp_bytes_malloc_dup_failed(amqp_bytes_t bytes) {
136 0 ▶ if (bytes.len != 0 && bytes.bytes == NULL) {
Branch: 2 branches
BranchTrueFalseTotal
if (bytes.len != 0 && bytes.bytes == NULL) { 0 0 0
if (bytes.len != 0 && bytes.bytes == NULL) { 0 0 0
137 0 return 1;
138 }
139 0 return 0;
140 }
141  
142 amqp_rpc_reply_t
143 0 amqp_consume_message(amqp_connection_state_t state, amqp_envelope_t *envelope,
144 struct timeval *timeout, AMQP_UNUSED int flags)
145 {
146 int res;
147 amqp_frame_t frame;
148 amqp_basic_deliver_t *delivery_method;
149 amqp_rpc_reply_t ret;
150  
151 0 memset(&ret, 0, sizeof(amqp_rpc_reply_t));
152 0 memset(envelope, 0, sizeof(amqp_envelope_t));
153  
154 0 res = amqp_simple_wait_frame_noblock(state, &frame, timeout);
155 0 ▶ if (AMQP_STATUS_OK != res) {
Branch: 1 branch
BranchTrueFalseTotal
if (AMQP_STATUS_OK != res) { 0 0 0
156 0 ret.reply_type = AMQP_RESPONSE_LIBRARY_EXCEPTION;
157 0 ret.library_error = res;
158 0 goto error_out1;
159 }
160  
161 0 ▶ if (AMQP_FRAME_METHOD != frame.frame_type
Branch: 1 branch
BranchTrueFalseTotal
if (AMQP_FRAME_METHOD != frame.frame_type 0 0 0
162 0 ▶ || AMQP_BASIC_DELIVER_METHOD != frame.payload.method.id) {
Branch: 1 branch
BranchTrueFalseTotal
|| AMQP_BASIC_DELIVER_METHOD != frame.payload.method.id) { 0 0 0
163 0 amqp_put_back_frame(state, &frame);
164 0 ret.reply_type = AMQP_RESPONSE_LIBRARY_EXCEPTION;
165 0 ret.library_error = AMQP_STATUS_UNEXPECTED_STATE;
166 0 goto error_out1;
167 }
168  
169 0 delivery_method = frame.payload.method.decoded;
170  
171 0 envelope->channel = frame.channel;
172 0 envelope->consumer_tag = amqp_bytes_malloc_dup(delivery_method->consumer_tag);
173 0 envelope->delivery_tag = delivery_method->delivery_tag;
174 0 envelope->redelivered = delivery_method->redelivered;
175 0 envelope->exchange = amqp_bytes_malloc_dup(delivery_method->exchange);
176 0 envelope->routing_key = amqp_bytes_malloc_dup(delivery_method->routing_key);
177  
178 0 if (amqp_bytes_malloc_dup_failed(envelope->consumer_tag) ||
179 0 ▶ amqp_bytes_malloc_dup_failed(envelope->exchange) ||
Branch: 1 branch
BranchTrueFalseTotal
amqp_bytes_malloc_dup_failed(envelope->exchange) || 0 0 0
180 0 amqp_bytes_malloc_dup_failed(envelope->routing_key)) {
181 0 ret.reply_type = AMQP_RESPONSE_LIBRARY_EXCEPTION;
182 0 ret.library_error = AMQP_STATUS_NO_MEMORY;
183 0 goto error_out2;
184 }
185  
186 0 ret = amqp_read_message(state, envelope->channel, &envelope->message, 0);
187 0 ▶ if (AMQP_RESPONSE_NORMAL != ret.reply_type) {
Branch: 1 branch
BranchTrueFalseTotal
if (AMQP_RESPONSE_NORMAL != ret.reply_type) { 0 0 0
188 0 goto error_out2;
189 }
190  
191 0 ret.reply_type = AMQP_RESPONSE_NORMAL;
192 0 return ret;
193  
194 0 error_out2:
195 0 amqp_bytes_free(envelope->routing_key);
196 0 amqp_bytes_free(envelope->exchange);
197 0 amqp_bytes_free(envelope->consumer_tag);
198 0 error_out1:
199 0 return ret;
200 }
201  
202 0 amqp_rpc_reply_t amqp_read_message(amqp_connection_state_t state,
203 amqp_channel_t channel,
204 amqp_message_t *message,
205 AMQP_UNUSED int flags)
206 {
207 amqp_frame_t frame;
208 amqp_rpc_reply_t ret;
209  
210 size_t body_read;
211 char *body_read_ptr;
212 int res;
213  
214 0 memset(&ret, 0, sizeof(amqp_rpc_reply_t));
215 0 memset(message, 0, sizeof(amqp_message_t));
216  
217 0 res = amqp_simple_wait_frame_on_channel(state, channel, &frame);
218 0 ▶ if (AMQP_STATUS_OK != res) {
Branch: 1 branch
BranchTrueFalseTotal
if (AMQP_STATUS_OK != res) { 0 0 0
219 0 ret.reply_type = AMQP_RESPONSE_LIBRARY_EXCEPTION;
220 0 ret.library_error = res;
221  
222 0 goto error_out1;
223 }
224  
225 0 ▶ if (AMQP_FRAME_HEADER != frame.frame_type) {
Branch: 1 branch
BranchTrueFalseTotal
if (AMQP_FRAME_HEADER != frame.frame_type) { 0 0 0
226 0 ▶ if (AMQP_FRAME_METHOD == frame.frame_type &&
Branch: 1 branch
BranchTrueFalseTotal
if (AMQP_FRAME_METHOD == frame.frame_type && 0 0 0
227 0 ▶ (AMQP_CHANNEL_CLOSE_METHOD == frame.payload.method.id ||
Branch: 1 branch
BranchTrueFalseTotal
(AMQP_CHANNEL_CLOSE_METHOD == frame.payload.method.id || 0 0 0
228 0 ▶ AMQP_CONNECTION_CLOSE_METHOD == frame.payload.method.id)) {
Branch: 1 branch
BranchTrueFalseTotal
AMQP_CONNECTION_CLOSE_METHOD == frame.payload.method.id)) { 0 0 0
229  
230 0 ret.reply_type = AMQP_RESPONSE_SERVER_EXCEPTION;
231 0 ret.reply = frame.payload.method;
232  
233 } else {
234 0 ret.reply_type = AMQP_RESPONSE_LIBRARY_EXCEPTION;
235 0 ret.library_error = AMQP_STATUS_UNEXPECTED_STATE;
236  
237 0 amqp_put_back_frame(state, &frame);
238 }
239 0 goto error_out1;
240 }
241  
242 0 init_amqp_pool(&message->pool, 4096);
243 0 res = amqp_basic_properties_clone(frame.payload.properties.decoded,
244 &message->properties, &message->pool);
245  
246 0 ▶ if (AMQP_STATUS_OK != res) {
Branch: 1 branch
BranchTrueFalseTotal
if (AMQP_STATUS_OK != res) { 0 0 0
247 0 ret.reply_type = AMQP_RESPONSE_LIBRARY_EXCEPTION;
248 0 ret.library_error = res;
249 0 goto error_out3;
250 }
251  
252 0 ▶ if (0 == frame.payload.properties.body_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 == frame.payload.properties.body_size) { 0 0 0
253 0 message->body = amqp_empty_bytes;
254 } else {
255 if (SIZE_MAX < frame.payload.properties.body_size) {
256 ret.reply_type = AMQP_RESPONSE_LIBRARY_EXCEPTION;
257 ret.library_error = AMQP_STATUS_NO_MEMORY;
258 goto error_out1;
259 }
260 0 message->body = amqp_bytes_malloc((size_t)frame.payload.properties.body_size);
261 0 ▶ if (NULL == message->body.bytes) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == message->body.bytes) { 0 0 0
262 0 ret.reply_type = AMQP_RESPONSE_LIBRARY_EXCEPTION;
263 0 ret.library_error = AMQP_STATUS_NO_MEMORY;
264 0 goto error_out1;
265 }
266 }
267  
268 0 body_read = 0;
269 0 body_read_ptr = message->body.bytes;
270  
271 0 ▶ while (body_read < message->body.len) {
Branch: 1 branch
BranchTrueFalseTotal
while (body_read < message->body.len) { 0 0 0
272 0 res = amqp_simple_wait_frame_on_channel(state, channel, &frame);
273 0 ▶ if (AMQP_STATUS_OK != res) {
Branch: 1 branch
BranchTrueFalseTotal
if (AMQP_STATUS_OK != res) { 0 0 0
274 0 ret.reply_type = AMQP_RESPONSE_LIBRARY_EXCEPTION;
275 0 ret.library_error = res;
276 0 goto error_out2;
277 }
278 0 ▶ if (AMQP_FRAME_BODY != frame.frame_type) {
Branch: 1 branch
BranchTrueFalseTotal
if (AMQP_FRAME_BODY != frame.frame_type) { 0 0 0
279 0 ▶ if (AMQP_FRAME_METHOD == frame.frame_type &&
Branch: 1 branch
BranchTrueFalseTotal
if (AMQP_FRAME_METHOD == frame.frame_type && 0 0 0
280 0 ▶ (AMQP_CHANNEL_CLOSE_METHOD == frame.payload.method.id ||
Branch: 1 branch
BranchTrueFalseTotal
(AMQP_CHANNEL_CLOSE_METHOD == frame.payload.method.id || 0 0 0
281 0 ▶ AMQP_CONNECTION_CLOSE_METHOD == frame.payload.method.id)) {
Branch: 1 branch
BranchTrueFalseTotal
AMQP_CONNECTION_CLOSE_METHOD == frame.payload.method.id)) { 0 0 0
282  
283 0 ret.reply_type = AMQP_RESPONSE_SERVER_EXCEPTION;
284 0 ret.reply = frame.payload.method;
285 } else {
286 0 ret.reply_type = AMQP_RESPONSE_LIBRARY_EXCEPTION;
287 0 ret.library_error = AMQP_STATUS_BAD_AMQP_DATA;
288 }
289 0 goto error_out2;
290 }
291  
292 0 ▶ if (body_read + frame.payload.body_fragment.len > message->body.len) {
Branch: 1 branch
BranchTrueFalseTotal
if (body_read + frame.payload.body_fragment.len > message->body.len) { 0 0 0
293 0 ret.reply_type = AMQP_RESPONSE_LIBRARY_EXCEPTION;
294 0 ret.library_error = AMQP_STATUS_BAD_AMQP_DATA;
295 0 goto error_out2;
296 }
297  
298 0 memcpy(body_read_ptr, frame.payload.body_fragment.bytes, frame.payload.body_fragment.len);
299  
300 0 body_read += frame.payload.body_fragment.len;
301 0 body_read_ptr += frame.payload.body_fragment.len;
302 }
303  
304 0 ret.reply_type = AMQP_RESPONSE_NORMAL;
305 0 return ret;
306  
307 0 error_out2:
308 0 amqp_bytes_free(message->body);
309 0 error_out3:
310 0 empty_amqp_pool(&message->pool);
311 0 error_out1:
312 0 return ret;
313 }
« blib/lib/Net/AMQP/RabbitMQ.pm ↑ Summary amqp_hostcheck.c »