Updated with Twitter's memcached_server_error() function.
[awesomized/libmemcached] / libmemcached / memcached_response.c
index 1ea0c725ea12ca94ad0446ff2d894e1554fd9960..ba0691b504a39590c67184d830e2c1753222cec4 100644 (file)
@@ -237,8 +237,34 @@ static memcached_return textual_read_one_response(memcached_server_st *ptr,
         memcached_server_response_increment(ptr);
         return MEMCACHED_STAT;
       }
-      else if (buffer[1] == 'E')
-        return MEMCACHED_SERVER_ERROR;
+      else if (buffer[1] == 'E') /* SERVER_ERROR */ 
+       {
+          char *rel_ptr;
+         char *startptr= buffer + 13, *endptr= startptr;
+
+         while (*endptr != '\r' && *endptr != '\n') endptr++;
+
+          /* 
+            Yes, we could make this "efficent" but to do that we would need
+            to maintain more state for the size of the buffer. Why waste
+            memory in the struct, which is important, for something that
+            rarely should happen?
+          */
+         rel_ptr= (char *)ptr->root->call_realloc(ptr->root, ptr->cached_server_error, endptr - startptr + 1);
+
+          if (rel_ptr == NULL)
+          {
+            /* If we happened to have some memory, we just null it since we don't know the size */
+            if (ptr->cached_server_error)
+              ptr->cached_server_error[0]= 0;
+            return MEMCACHED_SERVER_ERROR;
+          }
+         ptr->cached_server_error= rel_ptr;
+
+         memcpy(ptr->cached_server_error, startptr, endptr - startptr);
+         ptr->cached_server_error[endptr - startptr]= 0;
+         return MEMCACHED_SERVER_ERROR;
+       }
       else if (buffer[1] == 'T')
         return MEMCACHED_STORED;
       else