/* Perform text transformations on the Mobwrite response so the client will understand it:
 * 1. Add a blank line to the end of the response.
 * 2. If the client has requested the Javascript output format,
 *    wrap the text in a JavaScript method call.
 */
static ngx_int_t
ngx_http_mobwrite_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
{
  ngx_http_mobwrite_ctx_t *ctx;
  ngx_chain_t *next, *last = NULL;
  int last_buf;

  ctx = (ngx_http_mobwrite_ctx_t *)ngx_http_get_module_ctx(r, ngx_http_mobwrite_module);
  if ((ctx == NULL) || ((ctx->format != MOBWRITE_FMT_TXT) && (ctx->format != MOBWRITE_FMT_JS))) {
    /* Not a Mobwrite request, so move on to the next output filter */
    return ngx_http_next_body_filter(r, in);
  }

  if (ctx->format == MOBWRITE_FMT_JS) {
    js_escape(in, r->pool);
  }

  /* Determine whether this filter chain contains the end of the response. */
  last_buf = 0;
  for (next = in; next != NULL; next = next->next) {
    last = next;
    if (next->buf->last_buf) {
      last_buf = 1;
    }
  }

  /* If we have encountered the end of the response, append a blank line.
     If the client requested the JavaScript output format, generate the
     closing method call syntax too. */
  if (last_buf) {
    ngx_str_t text = ngx_string("\n");
    ngx_str_t js_text = ngx_string("\");\n");
    ngx_chain_t *post;
    if (ctx->format == MOBWRITE_FMT_JS) {
      post = str_to_buffer_chain(js_text, r->pool);
    }
    else {
      post = str_to_buffer_chain(text, r->pool);
    }
    last->buf->last_buf = 0;
    post->buf->last_buf = 1;
    last->next = post;
  }

  /* If the client requested the Javascript output format,
     wrap the text in a method call */
  if (!ctx->response_started) {
    if (ctx->format == MOBWRITE_FMT_JS) {
      ngx_str_t text = ngx_string("mobwrite.callback(\"");
      ngx_chain_t *pre = str_to_buffer_chain(text, r->pool);
      pre->next = in;
      in = pre;
    }
    ctx->response_started = 1;
  }

  /* Pass the buffer chain on to the next filter */
  return ngx_http_next_body_filter(r, in);
}
Esempio n. 2
0
File: js.c Progetto: Daisho/showtime
static JSBool 
js_paramEscape(JSContext *cx, JSObject *obj,
	      uintN argc, jsval *argv, jsval *rval)
{
  return js_escape(cx, obj, argc, argv, rval, URL_ESCAPE_PARAM);
}