Assert in debug to look for random allocation in get.
[awesomized/libmemcached] / lib / memcached_response.c
1 /*
2 Memcached library
3
4 memcached_response() is used to determine the return result
5 from an issued command.
6 */
7
8 #include "common.h"
9 #include "memcached_io.h"
10
11 memcached_return memcached_response(memcached_server_st *ptr,
12 char *buffer, size_t buffer_length,
13 memcached_result_st *result)
14 {
15 unsigned int x;
16 size_t send_length;
17 char *buffer_ptr;
18 unsigned int max_messages;
19
20
21 send_length= 0;
22 /* UDP at the moment is odd...*/
23 if (ptr->type == MEMCACHED_CONNECTION_UDP)
24 {
25 char buffer[8];
26 ssize_t read_length;
27
28 return MEMCACHED_SUCCESS;
29
30 read_length= memcached_io_read(ptr, buffer, 8);
31 }
32
33 /* We may have old commands in the buffer not set, first purge */
34 if (ptr->root->flags & MEM_NO_BLOCK)
35 (void)memcached_io_write(ptr, NULL, 0, 1);
36
37 max_messages= memcached_server_response_count(ptr);
38 for (x= 0; x < max_messages; x++)
39 {
40 size_t total_length= 0;
41 buffer_ptr= buffer;
42
43
44 while (1)
45 {
46 ssize_t read_length;
47
48 read_length= memcached_io_read(ptr, buffer_ptr, 1);
49 WATCHPOINT_ASSERT(isgraph(*buffer_ptr) || isspace(*buffer_ptr));
50
51 if (read_length != 1)
52 return MEMCACHED_UNKNOWN_READ_FAILURE;
53
54 if (*buffer_ptr == '\n')
55 break;
56 else
57 buffer_ptr++;
58
59 total_length++;
60 WATCHPOINT_ASSERT(total_length <= buffer_length);
61
62 if (total_length >= buffer_length)
63 return MEMCACHED_PROTOCOL_ERROR;
64 }
65 buffer_ptr++;
66 *buffer_ptr= 0;
67
68 memcached_server_response_decrement(ptr);
69 }
70
71 switch(buffer[0])
72 {
73 case 'V': /* VALUE || VERSION */
74 if (buffer[1] == 'A') /* VALUE */
75 {
76 memcached_return rc;
77
78 /* We add back in one because we will need to search for END */
79 memcached_server_response_increment(ptr);
80 if (result)
81 rc= value_fetch(ptr, buffer, result);
82 else
83 rc= value_fetch(ptr, buffer, &ptr->root->result);
84
85 return rc;
86 }
87 else if (buffer[1] == 'E') /* VERSION */
88 {
89 return MEMCACHED_SUCCESS;
90 }
91 else
92 {
93 WATCHPOINT_STRING(buffer);
94 WATCHPOINT_ASSERT(0);
95 return MEMCACHED_UNKNOWN_READ_FAILURE;
96 }
97 case 'O': /* OK */
98 return MEMCACHED_SUCCESS;
99 case 'S': /* STORED STATS SERVER_ERROR */
100 {
101 if (buffer[2] == 'A') /* STORED STATS */
102 {
103 memcached_server_response_increment(ptr);
104 return MEMCACHED_STAT;
105 }
106 else if (buffer[1] == 'E')
107 return MEMCACHED_SERVER_ERROR;
108 else if (buffer[1] == 'T')
109 return MEMCACHED_STORED;
110 else
111 {
112 WATCHPOINT_STRING(buffer);
113 WATCHPOINT_ASSERT(0);
114 return MEMCACHED_UNKNOWN_READ_FAILURE;
115 }
116 }
117 case 'D': /* DELETED */
118 return MEMCACHED_DELETED;
119 case 'N': /* NOT_FOUND */
120 {
121 if (buffer[4] == 'F')
122 return MEMCACHED_NOTFOUND;
123 else if (buffer[4] == 'S')
124 return MEMCACHED_NOTSTORED;
125 else
126 return MEMCACHED_UNKNOWN_READ_FAILURE;
127 }
128 case 'E': /* PROTOCOL ERROR or END */
129 {
130 if (buffer[1] == 'N')
131 return MEMCACHED_END;
132 else if (buffer[1] == 'R')
133 return MEMCACHED_PROTOCOL_ERROR;
134 else
135 return MEMCACHED_UNKNOWN_READ_FAILURE;
136 }
137 case 'C': /* CLIENT ERROR */
138 return MEMCACHED_CLIENT_ERROR;
139 default:
140 return MEMCACHED_UNKNOWN_READ_FAILURE;
141
142 }
143
144 return MEMCACHED_SUCCESS;
145 }
146
147 char *memcached_result_value(memcached_result_st *ptr)
148 {
149 memcached_string_st *sptr= &ptr->value;
150 return memcached_string_value(sptr);
151 }
152
153 size_t memcached_result_length(memcached_result_st *ptr)
154 {
155 memcached_string_st *sptr= &ptr->value;
156 return memcached_string_length(sptr);
157 }