[Groonga-commit] groonga/groonga at 475c12f [master] http/httpd: support incomplete JSON

Back to archive index

Kouhei Sutou null+****@clear*****
Tue Sep 6 15:22:54 JST 2016


Kouhei Sutou	2016-09-06 15:22:54 +0900 (Tue, 06 Sep 2016)

  New Revision: 475c12ff4e2f431f23d8fa7e5cdf89ef14c362e4
  https://github.com/groonga/groonga/commit/475c12ff4e2f431f23d8fa7e5cdf89ef14c362e4

  Message:
    http/httpd: support incomplete JSON
    
    If POST-ed JSON is incomplete, passed JSON is only processed.

  Modified files:
    lib/ctx.c
    lib/grn_ctx_impl.h
    lib/proc.c
    lib/proc/proc_select.c
    src/groonga.c
    src/httpd/nginx-module/ngx_http_groonga_module.c

  Modified: lib/ctx.c (+11 -8)
===================================================================
--- lib/ctx.c    2016-09-06 14:10:28 +0900 (27a5ba4)
+++ lib/ctx.c    2016-09-06 15:22:54 +0900 (f63dd1c)
@@ -243,9 +243,9 @@ grn_ctx_impl_init(grn_ctx *ctx)
   GRN_UINT32_INIT(&ctx->impl->output.levels, GRN_OBJ_VECTOR);
 
   if (ctx == &grn_gctx) {
-    ctx->impl->command_version = GRN_COMMAND_VERSION_STABLE;
+    ctx->impl->command.version = GRN_COMMAND_VERSION_STABLE;
   } else {
-    ctx->impl->command_version = grn_get_default_command_version();
+    ctx->impl->command.version = grn_get_default_command_version();
   }
 
   if (ctx == &grn_gctx) {
@@ -755,7 +755,7 @@ grn_command_version
 grn_ctx_get_command_version(grn_ctx *ctx)
 {
   if (ctx->impl) {
-    return ctx->impl->command_version;
+    return ctx->impl->command.version;
   } else {
     return GRN_COMMAND_VERSION_STABLE;
   }
@@ -766,12 +766,12 @@ grn_ctx_set_command_version(grn_ctx *ctx, grn_command_version version)
 {
   switch (version) {
   case GRN_COMMAND_VERSION_DEFAULT :
-    ctx->impl->command_version = GRN_COMMAND_VERSION_STABLE;
+    ctx->impl->command.version = GRN_COMMAND_VERSION_STABLE;
     return GRN_SUCCESS;
   default :
     if (GRN_COMMAND_VERSION_MIN <= version &&
         version <= GRN_COMMAND_VERSION_MAX) {
-      ctx->impl->command_version = version;
+      ctx->impl->command.version = version;
       return GRN_SUCCESS;
     } else {
       return GRN_UNSUPPORTED_COMMAND_VERSION;
@@ -1273,12 +1273,14 @@ grn_ctx_send(grn_ctx *ctx, const char *str, unsigned int str_len, int flags)
   if (!ctx) { return 0; }
   GRN_API_ENTER;
   if (ctx->impl) {
+    if ((flags & GRN_CTX_MORE)) { flags |= GRN_CTX_QUIET; }
+    if (ctx->stat == GRN_CTX_QUIT) { flags |= GRN_CTX_QUIT; }
+
+    ctx->impl->command.flags = flags;
     if (ctx->impl->com) {
       grn_rc rc;
       grn_com_header sheader;
       grn_timeval_now(ctx, &ctx->impl->tv);
-      if ((flags & GRN_CTX_MORE)) { flags |= GRN_CTX_QUIET; }
-      if (ctx->stat == GRN_CTX_QUIT) { flags |= GRN_CTX_QUIT; }
       sheader.proto = GRN_COM_PROTO_GQTP;
       sheader.qtype = 0;
       sheader.keylen = 0;
@@ -1340,7 +1342,8 @@ grn_ctx_send(grn_ctx *ctx, const char *str, unsigned int str_len, int flags)
                       "<", "rc=%d", ctx->rc);
       }
     output :
-      if (!(flags & GRN_CTX_QUIET) && ctx->impl->output.func) {
+      if (!(ctx->impl->command.flags & GRN_CTX_QUIET) &&
+          ctx->impl->output.func) {
         ctx->impl->output.func(ctx, GRN_CTX_TAIL, ctx->impl->output.data.ptr);
       }
       if (expr) { grn_expr_clear_vars(ctx, expr); }

  Modified: lib/grn_ctx_impl.h (+4 -2)
===================================================================
--- lib/grn_ctx_impl.h    2016-09-06 14:10:28 +0900 (8b76ba1)
+++ lib/grn_ctx_impl.h    2016-09-06 15:22:54 +0900 (42be6d0)
@@ -193,8 +193,10 @@ struct _grn_ctx_impl {
 #endif
   } output;
 
-  /* command portion */
-  grn_command_version command_version;
+  struct {
+    int flags;
+    grn_command_version version;
+  } command;
 
   /* match escalation portion */
   int64_t match_escalation_threshold;

  Modified: lib/proc.c (+2 -1)
===================================================================
--- lib/proc.c    2016-09-06 14:10:28 +0900 (6e9a6f6)
+++ lib/proc.c    2016-09-06 15:22:54 +0900 (8dd6a49)
@@ -142,7 +142,8 @@ proc_load(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data)
     ctx->impl->loader.stat = GRN_LOADER_END;
     ctx->impl->loader.rc = GRN_SUCCESS;
   }
-  if (ctx->impl->loader.stat != GRN_LOADER_END) {
+  if (ctx->impl->loader.stat != GRN_LOADER_END &&
+      !(ctx->impl->command.flags & GRN_CTX_TAIL)) {
     grn_ctx_set_next_expr(ctx, grn_proc_get_info(ctx, user_data, NULL, NULL, NULL));
   } else {
     if (ctx->impl->loader.rc != GRN_SUCCESS) {

  Modified: lib/proc/proc_select.c (+1 -1)
===================================================================
--- lib/proc/proc_select.c    2016-09-06 14:10:28 +0900 (59ad99e)
+++ lib/proc/proc_select.c    2016-09-06 15:22:54 +0900 (a78dbde)
@@ -2934,7 +2934,7 @@ grn_select(grn_ctx *ctx, grn_select_data *data)
     cp += sizeof(int);
     grn_memcpy(cp, &(data->limit), sizeof(int));
     cp += sizeof(int);
-    grn_memcpy(cp, &(ctx->impl->command_version), sizeof(grn_command_version));
+    grn_memcpy(cp, &(ctx->impl->command.version), sizeof(grn_command_version));
     cp += sizeof(grn_command_version);
     grn_memcpy(cp, &(ctx->impl->output.is_pretty), sizeof(grn_bool));
     cp += sizeof(grn_bool);

  Modified: src/groonga.c (+5 -3)
===================================================================
--- src/groonga.c    2016-09-06 14:10:28 +0900 (317d82c)
+++ src/groonga.c    2016-09-06 15:22:54 +0900 (522e9ef)
@@ -1511,7 +1511,7 @@ do_htreq_post(grn_ctx *ctx, ht_context *hc)
     return;
   }
 
-  grn_ctx_send(ctx, header.path_start, header.path_length, GRN_CTX_QUIET);
+  grn_ctx_send(ctx, header.path_start, header.path_length, GRN_CTX_MORE);
   if (ctx->rc != GRN_SUCCESS) {
     ht_context context;
     context.msg = msg;
@@ -1586,7 +1586,9 @@ do_htreq_post(grn_ctx *ctx, ht_context *hc)
           int flags = 0;
           if (!(read_content_length == header.content_length &&
                 buffer_current + 1 == buffer_end)) {
-            flags |= GRN_CTX_QUIET;
+            flags |= GRN_CTX_MORE;
+          } else {
+            flags |= GRN_CTX_TAIL;
           }
           grn_ctx_send(ctx,
                        GRN_TEXT_VALUE(&chunk_buffer),
@@ -1614,7 +1616,7 @@ do_htreq_post(grn_ctx *ctx, ht_context *hc)
       grn_ctx_send(ctx,
                    GRN_TEXT_VALUE(&chunk_buffer),
                    GRN_TEXT_LEN(&chunk_buffer),
-                   0);
+                   GRN_CTX_TAIL);
     }
 
     GRN_OBJ_FIN(ctx, &chunk_buffer);

  Modified: src/httpd/nginx-module/ngx_http_groonga_module.c (+6 -2)
===================================================================
--- src/httpd/nginx-module/ngx_http_groonga_module.c    2016-09-06 14:10:28 +0900 (c7f6f93)
+++ src/httpd/nginx-module/ngx_http_groonga_module.c    2016-09-06 15:22:54 +0900 (0338ec7)
@@ -800,13 +800,17 @@ ngx_http_groonga_send_body(ngx_http_request_t *r,
              line_current < line_end;
              line_current++) {
           size_t line_length;
+          int flags = GRN_NO_FLAGS;
 
           if (*line_current != '\n') {
             continue;
           }
 
           line_length = line_current - line_start + 1;
-          grn_ctx_send(context, line_start, line_length, GRN_NO_FLAGS);
+          if (!chain->next && rest_buffer_size == 0) {
+            flags |= GRN_CTX_TAIL;
+          }
+          grn_ctx_send(context, line_start, line_length, flags);
           line_start_offset += line_length;
           line_start += line_length;
           ngx_http_groonga_context_log_error(log);
@@ -840,7 +844,7 @@ ngx_http_groonga_send_body(ngx_http_request_t *r,
     grn_ctx_send(context,
                  GRN_TEXT_VALUE(&line_buffer),
                  GRN_TEXT_LEN(&line_buffer),
-                 GRN_NO_FLAGS);
+                 GRN_CTX_TAIL);
     ngx_http_groonga_context_log_error(log);
     if (context->rc != GRN_SUCCESS && data->rc == GRN_SUCCESS) {
       data->rc = context->rc;
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index