关键词搜索

源码搜索 ×
×

漫话Redis源码之五十八

发布2022-01-09浏览431次

详情内容

这里主要跟展示有关,说白了,就是format, 非核心内容:

  1. /*
  2. * Copyright (c) 2019, Salvatore Sanfilippo <antirez at gmail dot com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * * Neither the name of Redis nor the names of its contributors may be used
  14. * to endorse or promote products derived from this software without
  15. * specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. * POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include "server.h"
  30. /* Emit an item in Gopher directory listing format:
  31. * <type><descr><TAB><selector><TAB><hostname><TAB><port>
  32. * If descr or selector are NULL, then the "(NULL)" string is used instead. */
  33. void addReplyGopherItem(client *c, const char *type, const char *descr,
  34. const char *selector, const char *hostname, int port)
  35. {
  36. sds item = sdscatfmt(sdsempty(),"%s%s\t%s\t%s\t%i\r\n",
  37. type, descr,
  38. selector ? selector : "(NULL)",
  39. hostname ? hostname : "(NULL)",
  40. port);
  41. addReplyProto(c,item,sdslen(item));
  42. sdsfree(item);
  43. }
  44. /* This is called by processInputBuffer() when an inline request is processed
  45. * with Gopher mode enabled, and the request happens to have zero or just one
  46. * argument. In such case we get the relevant key and reply using the Gopher
  47. * protocol. */
  48. void processGopherRequest(client *c) {
  49. robj *keyname = c->argc == 0 ? createStringObject("/",1) : c->argv[0];
  50. robj *o = lookupKeyRead(c->db,keyname);
  51. /* If there is no such key, return with a Gopher error. */
  52. if (o == NULL || o->type != OBJ_STRING) {
  53. char *errstr;
  54. if (o == NULL)
  55. errstr = "Error: no content at the specified key";
  56. else
  57. errstr = "Error: selected key type is invalid "
  58. "for Gopher output";
  59. addReplyGopherItem(c,"i",errstr,NULL,NULL,0);
  60. addReplyGopherItem(c,"i","Redis Gopher server",NULL,NULL,0);
  61. } else {
  62. addReply(c,o);
  63. }
  64. /* Cleanup, also make sure to emit the final ".CRLF" line. Note that
  65. * the connection will be closed immediately after this because the client
  66. * will be flagged with CLIENT_CLOSE_AFTER_REPLY, in accordance with the
  67. * Gopher protocol. */
  68. if (c->argc == 0) decrRefCount(keyname);
  69. /* Note that in theory we should terminate the Gopher request with
  70. * ".<CR><LF>" (called Lastline in the RFC) like that:
  71. *
  72. * addReplyProto(c,".\r\n",3);
  73. *
  74. * However after examining the current clients landscape, it's probably
  75. * going to do more harm than good for several reasons:
  76. *
  77. * 1. Clients should not have any issue with missing .<CR><LF> as for
  78. * specification, and in the real world indeed certain servers
  79. * implementations never used to send the terminator.
  80. *
  81. * 2. Redis does not know if it's serving a text file or a binary file:
  82. * at the same time clients will not remove the ".<CR><LF>" bytes at
  83. * tne end when downloading a binary file from the server, so adding
  84. * the "Lastline" terminator without knowing the content is just
  85. * dangerous.
  86. *
  87. * 3. The utility gopher2redis.rb that we provide for Redis, and any
  88. * other similar tool you may use as Gopher authoring system for
  89. * Redis, can just add the "Lastline" when needed.
  90. */
  91. }

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载