4b29a1cbbec259211ef3800954a580f13f4512f9
[m6w6/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
23 /* We may have old commands in the buffer not set, first purge */
24 if (ptr->root->flags & MEM_NO_BLOCK)
25 (void)memcached_io_write(ptr, NULL, 0, 1);
26
27 max_messages= memcached_server_response_count(ptr);
28 for (x= 0; x < max_messages; x++)
29 {
30 size_t total_length= 0;
31 buffer_ptr= buffer;
32
33 while (1)
34 {
35 unsigned int read_length;
36
37 read_length= memcached_io_read(ptr, buffer_ptr, 1);
38
39 if (read_length != 1)
40 return MEMCACHED_UNKNOWN_READ_FAILURE;
41
42 if (*buffer_ptr == '\n')
43 break;
44 else
45 buffer_ptr++;
46
47 total_length++;
48 WATCHPOINT_ASSERT(total_length <= buffer_length);
49
50 if (total_length >= buffer_length)
51 return MEMCACHED_PROTOCOL_ERROR;
52 }
53 buffer_ptr++;
54 *buffer_ptr= 0;
55
56 memcached_server_response_decrement(ptr);
57 }
58
59 switch(buffer[0])
60 {
61 case 'V': /* VALUE || VERSION */
62 if (buffer[1] == 'A') /* VALUE */
63 {
64 memcached_return rc;
65
66 /* We add back in one because we will need to search for END */
67 memcached_server_response_increment(ptr);
68 if (result)
69 rc= value_fetch(ptr, buffer, result);
70 else
71 rc= value_fetch(ptr, buffer, &ptr->root->result);
72
73 return rc;
74 }
75 else if (buffer[1] == 'E') /* VERSION */
76 {
77 return MEMCACHED_SUCCESS;
78 }
79 else
80 {
81 WATCHPOINT_STRING(buffer);
82 WATCHPOINT_ASSERT(0);
83 return MEMCACHED_UNKNOWN_READ_FAILURE;
84 }
85 case 'O': /* OK */
86 return MEMCACHED_SUCCESS;
87 case 'S': /* STORED STATS SERVER_ERROR */
88 {
89 if (buffer[2] == 'A') /* STORED STATS */
90 {
91 memcached_server_response_increment(ptr);
92 return MEMCACHED_STAT;
93 }
94 else if (buffer[1] == 'E')
95 return MEMCACHED_SERVER_ERROR;
96 else if (buffer[1] == 'T')
97 return MEMCACHED_STORED;
98 else
99 {
100 WATCHPOINT_STRING(buffer);
101 WATCHPOINT_ASSERT(0);
102 return MEMCACHED_UNKNOWN_READ_FAILURE;
103 }
104 }
105 case 'D': /* DELETED */
106 return MEMCACHED_DELETED;
107 case 'N': /* NOT_FOUND */
108 {
109 if (buffer[4] == 'F')
110 return MEMCACHED_NOTFOUND;
111 else if (buffer[4] == 'S')
112 return MEMCACHED_NOTSTORED;
113 else
114 return MEMCACHED_UNKNOWN_READ_FAILURE;
115 }
116 case 'E': /* PROTOCOL ERROR or END */
117 {
118 if (buffer[1] == 'N')
119 return MEMCACHED_END;
120 else if (buffer[1] == 'R')
121 return MEMCACHED_PROTOCOL_ERROR;
122 else
123 return MEMCACHED_UNKNOWN_READ_FAILURE;
124 }
125 case 'C': /* CLIENT ERROR */
126 return MEMCACHED_CLIENT_ERROR;
127 default:
128 return MEMCACHED_UNKNOWN_READ_FAILURE;
129
130 }
131
132 return MEMCACHED_SUCCESS;
133 }
134
135 char *memcached_result_value(memcached_result_st *ptr)
136 {
137 memcached_string_st *sptr= &ptr->value;
138 return memcached_string_value(sptr);
139 }
140
141 size_t memcached_result_length(memcached_result_st *ptr)
142 {
143 memcached_string_st *sptr= &ptr->value;
144 return memcached_string_length(sptr);
145 }