]> git.saurik.com Git - redis.git/blob - src/networking.c
e2e25207b5d025b49063ad58450478d38121d1b1
[redis.git] / src / networking.c
1 #include "redis.h"
2 #include <sys/uio.h>
3
4 void *dupClientReplyValue(void *o) {
5 incrRefCount((robj*)o);
6 return o;
7 }
8
9 int listMatchObjects(void *a, void *b) {
10 return equalStringObjects(a,b);
11 }
12
13 redisClient *createClient(int fd) {
14 redisClient *c = zmalloc(sizeof(redisClient));
15 c->bufpos = 0;
16
17 anetNonBlock(NULL,fd);
18 anetTcpNoDelay(NULL,fd);
19 if (!c) return NULL;
20 if (aeCreateFileEvent(server.el,fd,AE_READABLE,
21 readQueryFromClient, c) == AE_ERR)
22 {
23 close(fd);
24 zfree(c);
25 return NULL;
26 }
27
28 selectDb(c,0);
29 c->fd = fd;
30 c->querybuf = sdsempty();
31 c->reqtype = 0;
32 c->argc = 0;
33 c->argv = NULL;
34 c->multibulklen = 0;
35 c->bulklen = -1;
36 c->sentlen = 0;
37 c->flags = 0;
38 c->lastinteraction = time(NULL);
39 c->authenticated = 0;
40 c->replstate = REDIS_REPL_NONE;
41 c->reply = listCreate();
42 listSetFreeMethod(c->reply,decrRefCount);
43 listSetDupMethod(c->reply,dupClientReplyValue);
44 c->blocking_keys = NULL;
45 c->blocking_keys_num = 0;
46 c->io_keys = listCreate();
47 c->watched_keys = listCreate();
48 listSetFreeMethod(c->io_keys,decrRefCount);
49 c->pubsub_channels = dictCreate(&setDictType,NULL);
50 c->pubsub_patterns = listCreate();
51 listSetFreeMethod(c->pubsub_patterns,decrRefCount);
52 listSetMatchMethod(c->pubsub_patterns,listMatchObjects);
53 listAddNodeTail(server.clients,c);
54 initClientMultiState(c);
55 return c;
56 }
57
58 int _installWriteEvent(redisClient *c) {
59 if (c->fd <= 0) return REDIS_ERR;
60 if (c->bufpos == 0 && listLength(c->reply) == 0 &&
61 (c->replstate == REDIS_REPL_NONE ||
62 c->replstate == REDIS_REPL_ONLINE) &&
63 aeCreateFileEvent(server.el, c->fd, AE_WRITABLE,
64 sendReplyToClient, c) == AE_ERR) return REDIS_ERR;
65 return REDIS_OK;
66 }
67
68 /* Create a duplicate of the last object in the reply list when
69 * it is not exclusively owned by the reply list. */
70 robj *dupLastObjectIfNeeded(list *reply) {
71 robj *new, *cur;
72 listNode *ln;
73 redisAssert(listLength(reply) > 0);
74 ln = listLast(reply);
75 cur = listNodeValue(ln);
76 if (cur->refcount > 1) {
77 new = dupStringObject(cur);
78 decrRefCount(cur);
79 listNodeValue(ln) = new;
80 }
81 return listNodeValue(ln);
82 }
83
84 int _addReplyToBuffer(redisClient *c, char *s, size_t len) {
85 size_t available = sizeof(c->buf)-c->bufpos;
86
87 /* If there already are entries in the reply list, we cannot
88 * add anything more to the static buffer. */
89 if (listLength(c->reply) > 0) return REDIS_ERR;
90
91 /* Check that the buffer has enough space available for this string. */
92 if (len > available) return REDIS_ERR;
93
94 memcpy(c->buf+c->bufpos,s,len);
95 c->bufpos+=len;
96 return REDIS_OK;
97 }
98
99 void _addReplyObjectToList(redisClient *c, robj *o) {
100 robj *tail;
101 if (listLength(c->reply) == 0) {
102 incrRefCount(o);
103 listAddNodeTail(c->reply,o);
104 } else {
105 tail = listNodeValue(listLast(c->reply));
106
107 /* Append to this object when possible. */
108 if (tail->ptr != NULL &&
109 sdslen(tail->ptr)+sdslen(o->ptr) <= REDIS_REPLY_CHUNK_BYTES)
110 {
111 tail = dupLastObjectIfNeeded(c->reply);
112 tail->ptr = sdscatlen(tail->ptr,o->ptr,sdslen(o->ptr));
113 } else {
114 incrRefCount(o);
115 listAddNodeTail(c->reply,o);
116 }
117 }
118 }
119
120 /* This method takes responsibility over the sds. When it is no longer
121 * needed it will be free'd, otherwise it ends up in a robj. */
122 void _addReplySdsToList(redisClient *c, sds s) {
123 robj *tail;
124 if (listLength(c->reply) == 0) {
125 listAddNodeTail(c->reply,createObject(REDIS_STRING,s));
126 } else {
127 tail = listNodeValue(listLast(c->reply));
128
129 /* Append to this object when possible. */
130 if (tail->ptr != NULL &&
131 sdslen(tail->ptr)+sdslen(s) <= REDIS_REPLY_CHUNK_BYTES)
132 {
133 tail = dupLastObjectIfNeeded(c->reply);
134 tail->ptr = sdscatlen(tail->ptr,s,sdslen(s));
135 sdsfree(s);
136 } else {
137 listAddNodeTail(c->reply,createObject(REDIS_STRING,s));
138 }
139 }
140 }
141
142 void _addReplyStringToList(redisClient *c, char *s, size_t len) {
143 robj *tail;
144 if (listLength(c->reply) == 0) {
145 listAddNodeTail(c->reply,createStringObject(s,len));
146 } else {
147 tail = listNodeValue(listLast(c->reply));
148
149 /* Append to this object when possible. */
150 if (tail->ptr != NULL &&
151 sdslen(tail->ptr)+len <= REDIS_REPLY_CHUNK_BYTES)
152 {
153 tail = dupLastObjectIfNeeded(c->reply);
154 tail->ptr = sdscatlen(tail->ptr,s,len);
155 } else {
156 listAddNodeTail(c->reply,createStringObject(s,len));
157 }
158 }
159 }
160
161 void addReply(redisClient *c, robj *obj) {
162 if (_installWriteEvent(c) != REDIS_OK) return;
163 redisAssert(!server.vm_enabled || obj->storage == REDIS_VM_MEMORY);
164
165 /* This is an important place where we can avoid copy-on-write
166 * when there is a saving child running, avoiding touching the
167 * refcount field of the object if it's not needed.
168 *
169 * If the encoding is RAW and there is room in the static buffer
170 * we'll be able to send the object to the client without
171 * messing with its page. */
172 if (obj->encoding == REDIS_ENCODING_RAW) {
173 if (_addReplyToBuffer(c,obj->ptr,sdslen(obj->ptr)) != REDIS_OK)
174 _addReplyObjectToList(c,obj);
175 } else {
176 obj = getDecodedObject(obj);
177 if (_addReplyToBuffer(c,obj->ptr,sdslen(obj->ptr)) != REDIS_OK)
178 _addReplyObjectToList(c,obj);
179 decrRefCount(obj);
180 }
181 }
182
183 void addReplySds(redisClient *c, sds s) {
184 if (_installWriteEvent(c) != REDIS_OK) {
185 /* The caller expects the sds to be free'd. */
186 sdsfree(s);
187 return;
188 }
189 if (_addReplyToBuffer(c,s,sdslen(s)) == REDIS_OK) {
190 sdsfree(s);
191 } else {
192 /* This method free's the sds when it is no longer needed. */
193 _addReplySdsToList(c,s);
194 }
195 }
196
197 void addReplyString(redisClient *c, char *s, size_t len) {
198 if (_installWriteEvent(c) != REDIS_OK) return;
199 if (_addReplyToBuffer(c,s,len) != REDIS_OK)
200 _addReplyStringToList(c,s,len);
201 }
202
203 void _addReplyError(redisClient *c, char *s, size_t len) {
204 addReplyString(c,"-ERR ",5);
205 addReplyString(c,s,len);
206 addReplyString(c,"\r\n",2);
207 }
208
209 void addReplyError(redisClient *c, char *err) {
210 _addReplyError(c,err,strlen(err));
211 }
212
213 void addReplyErrorFormat(redisClient *c, const char *fmt, ...) {
214 va_list ap;
215 va_start(ap,fmt);
216 sds s = sdscatvprintf(sdsempty(),fmt,ap);
217 va_end(ap);
218 _addReplyError(c,s,sdslen(s));
219 sdsfree(s);
220 }
221
222 void _addReplyStatus(redisClient *c, char *s, size_t len) {
223 addReplyString(c,"+",1);
224 addReplyString(c,s,len);
225 addReplyString(c,"\r\n",2);
226 }
227
228 void addReplyStatus(redisClient *c, char *status) {
229 _addReplyStatus(c,status,strlen(status));
230 }
231
232 void addReplyStatusFormat(redisClient *c, const char *fmt, ...) {
233 va_list ap;
234 va_start(ap,fmt);
235 sds s = sdscatvprintf(sdsempty(),fmt,ap);
236 va_end(ap);
237 _addReplyStatus(c,s,sdslen(s));
238 sdsfree(s);
239 }
240
241 /* Adds an empty object to the reply list that will contain the multi bulk
242 * length, which is not known when this function is called. */
243 void *addDeferredMultiBulkLength(redisClient *c) {
244 /* Note that we install the write event here even if the object is not
245 * ready to be sent, since we are sure that before returning to the
246 * event loop setDeferredMultiBulkLength() will be called. */
247 if (_installWriteEvent(c) != REDIS_OK) return NULL;
248 listAddNodeTail(c->reply,createObject(REDIS_STRING,NULL));
249 return listLast(c->reply);
250 }
251
252 /* Populate the length object and try glueing it to the next chunk. */
253 void setDeferredMultiBulkLength(redisClient *c, void *node, long length) {
254 listNode *ln = (listNode*)node;
255 robj *len, *next;
256
257 /* Abort when *node is NULL (see addDeferredMultiBulkLength). */
258 if (node == NULL) return;
259
260 len = listNodeValue(ln);
261 len->ptr = sdscatprintf(sdsempty(),"*%ld\r\n",length);
262 if (ln->next != NULL) {
263 next = listNodeValue(ln->next);
264
265 /* Only glue when the next node is non-NULL (an sds in this case) */
266 if (next->ptr != NULL) {
267 len->ptr = sdscatlen(len->ptr,next->ptr,sdslen(next->ptr));
268 listDelNode(c->reply,ln->next);
269 }
270 }
271 }
272
273 void addReplyDouble(redisClient *c, double d) {
274 char dbuf[128], sbuf[128];
275 int dlen, slen;
276 dlen = snprintf(dbuf,sizeof(dbuf),"%.17g",d);
277 slen = snprintf(sbuf,sizeof(sbuf),"$%d\r\n%s\r\n",dlen,dbuf);
278 addReplyString(c,sbuf,slen);
279 }
280
281 void _addReplyLongLong(redisClient *c, long long ll, char prefix) {
282 char buf[128];
283 int len;
284 buf[0] = prefix;
285 len = ll2string(buf+1,sizeof(buf)-1,ll);
286 buf[len+1] = '\r';
287 buf[len+2] = '\n';
288 addReplyString(c,buf,len+3);
289 }
290
291 void addReplyLongLong(redisClient *c, long long ll) {
292 _addReplyLongLong(c,ll,':');
293 }
294
295 void addReplyMultiBulkLen(redisClient *c, long length) {
296 _addReplyLongLong(c,length,'*');
297 }
298
299 void addReplyBulkLen(redisClient *c, robj *obj) {
300 size_t len;
301
302 if (obj->encoding == REDIS_ENCODING_RAW) {
303 len = sdslen(obj->ptr);
304 } else {
305 long n = (long)obj->ptr;
306
307 /* Compute how many bytes will take this integer as a radix 10 string */
308 len = 1;
309 if (n < 0) {
310 len++;
311 n = -n;
312 }
313 while((n = n/10) != 0) {
314 len++;
315 }
316 }
317 _addReplyLongLong(c,len,'$');
318 }
319
320 void addReplyBulk(redisClient *c, robj *obj) {
321 addReplyBulkLen(c,obj);
322 addReply(c,obj);
323 addReply(c,shared.crlf);
324 }
325
326 /* In the CONFIG command we need to add vanilla C string as bulk replies */
327 void addReplyBulkCString(redisClient *c, char *s) {
328 if (s == NULL) {
329 addReply(c,shared.nullbulk);
330 } else {
331 robj *o = createStringObject(s,strlen(s));
332 addReplyBulk(c,o);
333 decrRefCount(o);
334 }
335 }
336
337 void acceptHandler(aeEventLoop *el, int fd, void *privdata, int mask) {
338 int cport, cfd;
339 char cip[128];
340 redisClient *c;
341 REDIS_NOTUSED(el);
342 REDIS_NOTUSED(mask);
343 REDIS_NOTUSED(privdata);
344
345 cfd = anetAccept(server.neterr, fd, cip, &cport);
346 if (cfd == AE_ERR) {
347 redisLog(REDIS_VERBOSE,"Accepting client connection: %s", server.neterr);
348 return;
349 }
350 redisLog(REDIS_VERBOSE,"Accepted %s:%d", cip, cport);
351 if ((c = createClient(cfd)) == NULL) {
352 redisLog(REDIS_WARNING,"Error allocating resoures for the client");
353 close(cfd); /* May be already closed, just ingore errors */
354 return;
355 }
356 /* If maxclient directive is set and this is one client more... close the
357 * connection. Note that we create the client instead to check before
358 * for this condition, since now the socket is already set in nonblocking
359 * mode and we can send an error for free using the Kernel I/O */
360 if (server.maxclients && listLength(server.clients) > server.maxclients) {
361 char *err = "-ERR max number of clients reached\r\n";
362
363 /* That's a best effort error message, don't check write errors */
364 if (write(c->fd,err,strlen(err)) == -1) {
365 /* Nothing to do, Just to avoid the warning... */
366 }
367 freeClient(c);
368 return;
369 }
370 server.stat_numconnections++;
371 }
372
373 static void freeClientArgv(redisClient *c) {
374 int j;
375 for (j = 0; j < c->argc; j++)
376 decrRefCount(c->argv[j]);
377 c->argc = 0;
378 }
379
380 void freeClient(redisClient *c) {
381 listNode *ln;
382
383 /* Note that if the client we are freeing is blocked into a blocking
384 * call, we have to set querybuf to NULL *before* to call
385 * unblockClientWaitingData() to avoid processInputBuffer() will get
386 * called. Also it is important to remove the file events after
387 * this, because this call adds the READABLE event. */
388 sdsfree(c->querybuf);
389 c->querybuf = NULL;
390 if (c->flags & REDIS_BLOCKED)
391 unblockClientWaitingData(c);
392
393 /* UNWATCH all the keys */
394 unwatchAllKeys(c);
395 listRelease(c->watched_keys);
396 /* Unsubscribe from all the pubsub channels */
397 pubsubUnsubscribeAllChannels(c,0);
398 pubsubUnsubscribeAllPatterns(c,0);
399 dictRelease(c->pubsub_channels);
400 listRelease(c->pubsub_patterns);
401 /* Obvious cleanup */
402 aeDeleteFileEvent(server.el,c->fd,AE_READABLE);
403 aeDeleteFileEvent(server.el,c->fd,AE_WRITABLE);
404 listRelease(c->reply);
405 freeClientArgv(c);
406 close(c->fd);
407 /* Remove from the list of clients */
408 ln = listSearchKey(server.clients,c);
409 redisAssert(ln != NULL);
410 listDelNode(server.clients,ln);
411 /* Remove from the list of clients waiting for swapped keys, or ready
412 * to be restarted, but not yet woken up again. */
413 if (c->flags & REDIS_IO_WAIT) {
414 redisAssert(server.vm_enabled);
415 if (listLength(c->io_keys) == 0) {
416 ln = listSearchKey(server.io_ready_clients,c);
417
418 /* When this client is waiting to be woken up (REDIS_IO_WAIT),
419 * it should be present in the list io_ready_clients */
420 redisAssert(ln != NULL);
421 listDelNode(server.io_ready_clients,ln);
422 } else {
423 while (listLength(c->io_keys)) {
424 ln = listFirst(c->io_keys);
425 dontWaitForSwappedKey(c,ln->value);
426 }
427 }
428 server.vm_blocked_clients--;
429 }
430 listRelease(c->io_keys);
431 /* Master/slave cleanup.
432 * Case 1: we lost the connection with a slave. */
433 if (c->flags & REDIS_SLAVE) {
434 if (c->replstate == REDIS_REPL_SEND_BULK && c->repldbfd != -1)
435 close(c->repldbfd);
436 list *l = (c->flags & REDIS_MONITOR) ? server.monitors : server.slaves;
437 ln = listSearchKey(l,c);
438 redisAssert(ln != NULL);
439 listDelNode(l,ln);
440 }
441
442 /* Case 2: we lost the connection with the master. */
443 if (c->flags & REDIS_MASTER) {
444 server.master = NULL;
445 server.replstate = REDIS_REPL_CONNECT;
446 /* Since we lost the connection with the master, we should also
447 * close the connection with all our slaves if we have any, so
448 * when we'll resync with the master the other slaves will sync again
449 * with us as well. Note that also when the slave is not connected
450 * to the master it will keep refusing connections by other slaves. */
451 while (listLength(server.slaves)) {
452 ln = listFirst(server.slaves);
453 freeClient((redisClient*)ln->value);
454 }
455 }
456 /* Release memory */
457 zfree(c->argv);
458 freeClientMultiState(c);
459 zfree(c);
460 }
461
462 void sendReplyToClient(aeEventLoop *el, int fd, void *privdata, int mask) {
463 redisClient *c = privdata;
464 int nwritten = 0, totwritten = 0, objlen;
465 robj *o;
466 REDIS_NOTUSED(el);
467 REDIS_NOTUSED(mask);
468
469 /* Use writev() if we have enough buffers to send */
470 if (!server.glueoutputbuf &&
471 listLength(c->reply) > REDIS_WRITEV_THRESHOLD &&
472 !(c->flags & REDIS_MASTER))
473 {
474 sendReplyToClientWritev(el, fd, privdata, mask);
475 return;
476 }
477
478 while(c->bufpos > 0 || listLength(c->reply)) {
479 if (c->bufpos > 0) {
480 if (c->flags & REDIS_MASTER) {
481 /* Don't reply to a master */
482 nwritten = c->bufpos - c->sentlen;
483 } else {
484 nwritten = write(fd,c->buf+c->sentlen,c->bufpos-c->sentlen);
485 if (nwritten <= 0) break;
486 }
487 c->sentlen += nwritten;
488 totwritten += nwritten;
489
490 /* If the buffer was sent, set bufpos to zero to continue with
491 * the remainder of the reply. */
492 if (c->sentlen == c->bufpos) {
493 c->bufpos = 0;
494 c->sentlen = 0;
495 }
496 } else {
497 o = listNodeValue(listFirst(c->reply));
498 objlen = sdslen(o->ptr);
499
500 if (objlen == 0) {
501 listDelNode(c->reply,listFirst(c->reply));
502 continue;
503 }
504
505 if (c->flags & REDIS_MASTER) {
506 /* Don't reply to a master */
507 nwritten = objlen - c->sentlen;
508 } else {
509 nwritten = write(fd, ((char*)o->ptr)+c->sentlen,objlen-c->sentlen);
510 if (nwritten <= 0) break;
511 }
512 c->sentlen += nwritten;
513 totwritten += nwritten;
514
515 /* If we fully sent the object on head go to the next one */
516 if (c->sentlen == objlen) {
517 listDelNode(c->reply,listFirst(c->reply));
518 c->sentlen = 0;
519 }
520 }
521 /* Note that we avoid to send more thank REDIS_MAX_WRITE_PER_EVENT
522 * bytes, in a single threaded server it's a good idea to serve
523 * other clients as well, even if a very large request comes from
524 * super fast link that is always able to accept data (in real world
525 * scenario think about 'KEYS *' against the loopback interfae) */
526 if (totwritten > REDIS_MAX_WRITE_PER_EVENT) break;
527 }
528 if (nwritten == -1) {
529 if (errno == EAGAIN) {
530 nwritten = 0;
531 } else {
532 redisLog(REDIS_VERBOSE,
533 "Error writing to client: %s", strerror(errno));
534 freeClient(c);
535 return;
536 }
537 }
538 if (totwritten > 0) c->lastinteraction = time(NULL);
539 if (listLength(c->reply) == 0) {
540 c->sentlen = 0;
541 aeDeleteFileEvent(server.el,c->fd,AE_WRITABLE);
542
543 /* Close connection after entire reply has been sent. */
544 if (c->flags & REDIS_QUIT) freeClient(c);
545 if (c->flags & REDIS_CLOSE_AFTER_REPLY) freeClient(c);
546 }
547 }
548
549 void sendReplyToClientWritev(aeEventLoop *el, int fd, void *privdata, int mask)
550 {
551 redisClient *c = privdata;
552 int nwritten = 0, totwritten = 0, objlen, willwrite;
553 robj *o;
554 struct iovec iov[REDIS_WRITEV_IOVEC_COUNT];
555 int offset, ion = 0;
556 REDIS_NOTUSED(el);
557 REDIS_NOTUSED(mask);
558
559 listNode *node;
560 while (listLength(c->reply)) {
561 offset = c->sentlen;
562 ion = 0;
563 willwrite = 0;
564
565 /* fill-in the iov[] array */
566 for(node = listFirst(c->reply); node; node = listNextNode(node)) {
567 o = listNodeValue(node);
568 objlen = sdslen(o->ptr);
569
570 if (totwritten + objlen - offset > REDIS_MAX_WRITE_PER_EVENT)
571 break;
572
573 if(ion == REDIS_WRITEV_IOVEC_COUNT)
574 break; /* no more iovecs */
575
576 iov[ion].iov_base = ((char*)o->ptr) + offset;
577 iov[ion].iov_len = objlen - offset;
578 willwrite += objlen - offset;
579 offset = 0; /* just for the first item */
580 ion++;
581 }
582
583 if(willwrite == 0)
584 break;
585
586 /* write all collected blocks at once */
587 if((nwritten = writev(fd, iov, ion)) < 0) {
588 if (errno != EAGAIN) {
589 redisLog(REDIS_VERBOSE,
590 "Error writing to client: %s", strerror(errno));
591 freeClient(c);
592 return;
593 }
594 break;
595 }
596
597 totwritten += nwritten;
598 offset = c->sentlen;
599
600 /* remove written robjs from c->reply */
601 while (nwritten && listLength(c->reply)) {
602 o = listNodeValue(listFirst(c->reply));
603 objlen = sdslen(o->ptr);
604
605 if(nwritten >= objlen - offset) {
606 listDelNode(c->reply, listFirst(c->reply));
607 nwritten -= objlen - offset;
608 c->sentlen = 0;
609 } else {
610 /* partial write */
611 c->sentlen += nwritten;
612 break;
613 }
614 offset = 0;
615 }
616 }
617
618 if (totwritten > 0)
619 c->lastinteraction = time(NULL);
620
621 if (listLength(c->reply) == 0) {
622 c->sentlen = 0;
623 aeDeleteFileEvent(server.el,c->fd,AE_WRITABLE);
624 }
625 }
626
627 /* resetClient prepare the client to process the next command */
628 void resetClient(redisClient *c) {
629 freeClientArgv(c);
630 c->reqtype = 0;
631 c->multibulklen = 0;
632 c->bulklen = -1;
633 }
634
635 void closeTimedoutClients(void) {
636 redisClient *c;
637 listNode *ln;
638 time_t now = time(NULL);
639 listIter li;
640
641 listRewind(server.clients,&li);
642 while ((ln = listNext(&li)) != NULL) {
643 c = listNodeValue(ln);
644 if (server.maxidletime &&
645 !(c->flags & REDIS_SLAVE) && /* no timeout for slaves */
646 !(c->flags & REDIS_MASTER) && /* no timeout for masters */
647 !(c->flags & REDIS_BLOCKED) && /* no timeout for BLPOP */
648 dictSize(c->pubsub_channels) == 0 && /* no timeout for pubsub */
649 listLength(c->pubsub_patterns) == 0 &&
650 (now - c->lastinteraction > server.maxidletime))
651 {
652 redisLog(REDIS_VERBOSE,"Closing idle client");
653 freeClient(c);
654 } else if (c->flags & REDIS_BLOCKED) {
655 if (c->blockingto != 0 && c->blockingto < now) {
656 addReply(c,shared.nullmultibulk);
657 unblockClientWaitingData(c);
658 }
659 }
660 }
661 }
662
663 int processInlineBuffer(redisClient *c) {
664 char *newline = strstr(c->querybuf,"\r\n");
665 int argc, j;
666 sds *argv;
667 size_t querylen;
668
669 /* Nothing to do without a \r\n */
670 if (newline == NULL)
671 return REDIS_ERR;
672
673 /* Split the input buffer up to the \r\n */
674 querylen = newline-(c->querybuf);
675 argv = sdssplitlen(c->querybuf,querylen," ",1,&argc);
676
677 /* Leave data after the first line of the query in the buffer */
678 c->querybuf = sdsrange(c->querybuf,querylen+2,-1);
679
680 /* Setup argv array on client structure */
681 if (c->argv) zfree(c->argv);
682 c->argv = zmalloc(sizeof(robj*)*argc);
683
684 /* Create redis objects for all arguments. */
685 for (c->argc = 0, j = 0; j < argc; j++) {
686 if (sdslen(argv[j])) {
687 c->argv[c->argc] = createObject(REDIS_STRING,argv[j]);
688 c->argc++;
689 } else {
690 sdsfree(argv[j]);
691 }
692 }
693 zfree(argv);
694 return REDIS_OK;
695 }
696
697 /* Helper function. Trims query buffer to make the function that processes
698 * multi bulk requests idempotent. */
699 static void setProtocolError(redisClient *c, int pos) {
700 c->flags |= REDIS_CLOSE_AFTER_REPLY;
701 c->querybuf = sdsrange(c->querybuf,pos,-1);
702 }
703
704 int processMultibulkBuffer(redisClient *c) {
705 char *newline = NULL;
706 char *eptr;
707 int pos = 0, tolerr;
708 long bulklen;
709
710 if (c->multibulklen == 0) {
711 /* The client should have been reset */
712 redisAssert(c->argc == 0);
713
714 /* Multi bulk length cannot be read without a \r\n */
715 newline = strstr(c->querybuf,"\r\n");
716 if (newline == NULL)
717 return REDIS_ERR;
718
719 /* We know for sure there is a whole line since newline != NULL,
720 * so go ahead and find out the multi bulk length. */
721 redisAssert(c->querybuf[0] == '*');
722 c->multibulklen = strtol(c->querybuf+1,&eptr,10);
723 pos = (newline-c->querybuf)+2;
724 if (c->multibulklen <= 0) {
725 c->querybuf = sdsrange(c->querybuf,pos,-1);
726 return REDIS_OK;
727 }
728
729 /* Setup argv array on client structure */
730 if (c->argv) zfree(c->argv);
731 c->argv = zmalloc(sizeof(robj*)*c->multibulklen);
732
733 /* Search new newline */
734 newline = strstr(c->querybuf+pos,"\r\n");
735 }
736
737 redisAssert(c->multibulklen > 0);
738 while(c->multibulklen) {
739 /* Read bulk length if unknown */
740 if (c->bulklen == -1) {
741 newline = strstr(c->querybuf+pos,"\r\n");
742 if (newline != NULL) {
743 if (c->querybuf[pos] != '$') {
744 addReplyErrorFormat(c,
745 "Protocol error: expected '$', got '%c'",
746 c->querybuf[pos]);
747 setProtocolError(c,pos);
748 return REDIS_ERR;
749 }
750
751 bulklen = strtol(c->querybuf+pos+1,&eptr,10);
752 tolerr = (eptr[0] != '\r');
753 if (tolerr || bulklen == LONG_MIN || bulklen == LONG_MAX ||
754 bulklen < 0 || bulklen > 1024*1024*1024)
755 {
756 addReplyError(c,"Protocol error: invalid bulk length");
757 setProtocolError(c,pos);
758 return REDIS_ERR;
759 }
760 pos += eptr-(c->querybuf+pos)+2;
761 c->bulklen = bulklen;
762 } else {
763 /* No newline in current buffer, so wait for more data */
764 break;
765 }
766 }
767
768 /* Read bulk argument */
769 if (sdslen(c->querybuf)-pos < (unsigned)(c->bulklen+2)) {
770 /* Not enough data (+2 == trailing \r\n) */
771 break;
772 } else {
773 c->argv[c->argc++] = createStringObject(c->querybuf+pos,c->bulklen);
774 pos += c->bulklen+2;
775 c->bulklen = -1;
776 c->multibulklen--;
777 }
778 }
779
780 /* Trim to pos */
781 c->querybuf = sdsrange(c->querybuf,pos,-1);
782
783 /* We're done when c->multibulk == 0 */
784 if (c->multibulklen == 0) {
785 return REDIS_OK;
786 }
787 return REDIS_ERR;
788 }
789
790 void processInputBuffer(redisClient *c) {
791 /* Keep processing while there is something in the input buffer */
792 while(sdslen(c->querybuf)) {
793 /* Before to process the input buffer, make sure the client is not
794 * waitig for a blocking operation such as BLPOP. Note that the first
795 * iteration the client is never blocked, otherwise the processInputBuffer
796 * would not be called at all, but after the execution of the first commands
797 * in the input buffer the client may be blocked, and the "goto again"
798 * will try to reiterate. The following line will make it return asap. */
799 if (c->flags & REDIS_BLOCKED || c->flags & REDIS_IO_WAIT) return;
800
801 /* Never continue to process the input buffer after QUIT. After the output
802 * buffer is flushed (with the OK), the connection will be dropped. */
803 if (c->flags & REDIS_QUIT) return;
804
805 /* Determine request type when unknown. */
806 if (!c->reqtype) {
807 if (c->querybuf[0] == '*') {
808 c->reqtype = REDIS_REQ_MULTIBULK;
809 } else {
810 c->reqtype = REDIS_REQ_INLINE;
811 }
812 }
813
814 if (c->reqtype == REDIS_REQ_INLINE) {
815 if (processInlineBuffer(c) != REDIS_OK) break;
816 } else if (c->reqtype == REDIS_REQ_MULTIBULK) {
817 if (processMultibulkBuffer(c) != REDIS_OK) break;
818 } else {
819 redisPanic("Unknown request type");
820 }
821
822 /* Multibulk processing could see a <= 0 length. */
823 if (c->argc == 0) {
824 resetClient(c);
825 } else {
826 /* Only reset the client when the command was executed. */
827 if (processCommand(c) == REDIS_OK)
828 resetClient(c);
829 }
830 }
831 }
832
833 void readQueryFromClient(aeEventLoop *el, int fd, void *privdata, int mask) {
834 redisClient *c = (redisClient*) privdata;
835 char buf[REDIS_IOBUF_LEN];
836 int nread;
837 REDIS_NOTUSED(el);
838 REDIS_NOTUSED(mask);
839
840 nread = read(fd, buf, REDIS_IOBUF_LEN);
841 if (nread == -1) {
842 if (errno == EAGAIN) {
843 nread = 0;
844 } else {
845 redisLog(REDIS_VERBOSE, "Reading from client: %s",strerror(errno));
846 freeClient(c);
847 return;
848 }
849 } else if (nread == 0) {
850 redisLog(REDIS_VERBOSE, "Client closed connection");
851 freeClient(c);
852 return;
853 }
854 if (nread) {
855 c->querybuf = sdscatlen(c->querybuf,buf,nread);
856 c->lastinteraction = time(NULL);
857 } else {
858 return;
859 }
860 processInputBuffer(c);
861 }