OSDN Git Service

* Added New Features.
authorAtsushi Konno <konn@users.sourceforge.jp>
Thu, 9 Apr 2009 22:41:34 +0000 (07:41 +0900)
committerAtsushi Konno <konn@users.sourceforge.jp>
Thu, 9 Apr 2009 22:41:34 +0000 (07:41 +0900)
    - http://sourceforge.jp/ticket/browse.php?group_id=1608&tid=16050

include/chxj_serf.h
src/chxj_apply_convrule.c
src/chxj_img_conv_format.c
src/chxj_jhtml.c
src/chxj_serf.c
src/mod_chxj.c

index b99984b..5fbc7f7 100644 (file)
@@ -44,6 +44,7 @@ extern char *(*chxj_serf_get)(request_rec *r, apr_pool_t *ppool, const char *url
 
 extern char *default_chxj_serf_post(request_rec *r, apr_pool_t *ppool, const char *url_path, char *post_data, apr_size_t post_data_len, int set_headers_flag, apr_size_t *res_len, int *response_code);
 extern char *(*chxj_serf_post)(request_rec *r, apr_pool_t *ppool, const char *url_path, char *post_data, apr_size_t post_data_len, int set_headers_flag, apr_size_t *res_len, int *response_code);
+extern apr_table_t *(*chxj_serf_head)(request_rec *r, apr_pool_t *ppool, const char *url_path, int *response_code);
 
 
 #endif
index 96d9706..475f448 100644 (file)
@@ -30,7 +30,7 @@ chxj_apply_convrule(request_rec *r, apr_array_header_t *convrules)
   for (ii = 0; ii < convrules->nelts; ii++) {
     pp = &entries[ii];
 
-    if (r->main != NULL) continue;
+    if (r->main) continue;
 
     /* Match */
     if (s_apply_rule(r, pp)) 
index c768283..6353c30 100644 (file)
@@ -1589,22 +1589,23 @@ s_img_down_sizing(MagickWand *magick_wand, request_rec *r, device_table *spec)
 
 
 static apr_status_t 
-s_send_cache_file(device_table *UNUSED(spec), query_string_param_t *query_string, request_rec *r, const char *tmpfile)
+s_send_cache_file(device_table *spec, query_string_param_t *query_string, request_rec *r, const char *tmpfile)
 {
   apr_status_t rv;
   apr_finfo_t  st;
   apr_file_t   *fout;
   apr_size_t   sendbyte;
   char         *contentLength;
+  mod_chxj_config *conf = ap_get_module_config(r->per_dir_config, &chxj_module);
 
   rv = apr_stat(&st, tmpfile, APR_FINFO_MIN, r->pool);
   if (rv != APR_SUCCESS)
     return HTTP_NOT_FOUND;
 
-  DBG(r, "mode:[%d]",    query_string->mode);
-  DBG(r, "name:[%s]",    query_string->name);
-  DBG(r, "offset:[%ld]", query_string->offset);
-  DBG(r, "count:[%ld]",  query_string->count);
+  DBG(r, "REQ[%X] mode:[%d]",    TO_ADDR(r), query_string->mode);
+  DBG(r, "REQ[%X] name:[%s]",    TO_ADDR(r), query_string->name);
+  DBG(r, "REQ[%X] offset:[%ld]", TO_ADDR(r), query_string->offset);
+  DBG(r, "REQ[%X] count:[%ld]",  TO_ADDR(r), query_string->count);
 
   if (query_string->mode != IMG_CONV_MODE_EZGET && query_string->name == NULL) {
     contentLength = apr_psprintf(r->pool, "%d", (int)st.size);
@@ -1645,6 +1646,12 @@ s_send_cache_file(device_table *UNUSED(spec), query_string_param_t *query_string
         return HTTP_NOT_FOUND;
       }
     }
+    if (conf->image_copyright) {
+      DBG(r, "REQ[%X] Add COPYRIGHT Header for SoftBank [%s]", TO_ADDR(r), conf->image_copyright);
+      if (spec->html_spec_type == CHXJ_SPEC_Jhtml ||  spec->html_spec_type == CHXJ_SPEC_Jxhtml) {
+        apr_table_setn(r->headers_out, "x-jphone-copyright", "no-transfer");
+      }
+    }
     rv = apr_file_open(&fout, tmpfile, 
       APR_READ | APR_BINARY, APR_OS_DEFAULT, r->pool);
     if (rv != APR_SUCCESS) {
@@ -1654,7 +1661,7 @@ s_send_cache_file(device_table *UNUSED(spec), query_string_param_t *query_string
     ap_send_fd(fout, r, 0, st.size, &sendbyte);
     apr_file_close(fout);
     ap_rflush(r);
-    DBG(r, "send file data[%d]byte", (int)sendbyte);
+    DBG(r, "REQ[%X] send file data[%d]byte", TO_ADDR(r), (int)sendbyte);
   }
   else
   if (query_string->mode == IMG_CONV_MODE_EZGET) {
@@ -1749,11 +1756,12 @@ s_send_original_file(request_rec *r, const char *originalfile)
 
 
 static apr_status_t 
-s_header_only_cache_file(device_table *UNUSED(spec), query_string_param_t *query_string, request_rec *r, const char *tmpfile)
+s_header_only_cache_file(device_table *spec, query_string_param_t *query_string, request_rec *r, const char *tmpfile)
 {
   apr_status_t rv;
   apr_finfo_t  st;
   char         *contentLength;
+  mod_chxj_config *conf = ap_get_module_config(r->per_dir_config, &chxj_module);
 
   DBG(r, "REQ[%X] start s_header_only_cache_file()", TO_ADDR(r));
 
@@ -1847,6 +1855,12 @@ s_header_only_cache_file(device_table *UNUSED(spec), query_string_param_t *query
       DBG(r, "REQ[%X] Content-Length:[%d]", TO_ADDR(r), (int)st.size);
     }
   }
+  if (conf->image_copyright) {
+    DBG(r, "REQ[%X] Add COPYRIGHT Header for SoftBank [%s]", TO_ADDR(r), conf->image_copyright);
+    if (spec->html_spec_type == CHXJ_SPEC_Jhtml ||  spec->html_spec_type == CHXJ_SPEC_Jxhtml) {
+      apr_table_setn(r->headers_out, "x-jphone-copyright", "no-transfer");
+    }
+  }
   
   DBG(r, "REQ[%X] end s_header_only_cache_file()", TO_ADDR(r));
   return OK;
index f5fb16d..99df87e 100644 (file)
@@ -24,6 +24,7 @@
 #include "chxj_str_util.h"
 #include "chxj_header_inf.h"
 #include "chxj_jreserved_tag.h"
+#include "chxj_serf.h"
 
 
 #define GET_JHTML(X) ((jhtml_t *)(X))
@@ -126,6 +127,7 @@ static int   s_jhtml_search_emoji(jhtml_t *jhtml, char *txt, char **rslt);
 
 static char *chxj_istyle_to_mode(apr_pool_t *p, const char *s);
 
+static char *s_add_copyright_parameter(request_rec *r, char *value);
 
 
 tag_handler jhtml_handler[] = {
@@ -2092,6 +2094,7 @@ s_jhtml_start_img_tag(void *pdoc, Node *node)
       value = chxj_jreserved_tag_to_safe_for_query_string(r, value, jhtml->entryp);
       value = chxj_add_cookie_parameter(r, value, jhtml->cookie);
       value = chxj_add_cookie_no_update_parameter(r, value);
+      value = s_add_copyright_parameter(r, value);
       W_L(" src=\"");
       W_V(value);
       W_L("\"");
@@ -2101,6 +2104,7 @@ s_jhtml_start_img_tag(void *pdoc, Node *node)
       value = chxj_jreserved_tag_to_safe_for_query_string(r, value, jhtml->entryp);
       value = chxj_add_cookie_parameter(r, value, jhtml->cookie);
       value = chxj_add_cookie_no_update_parameter(r, value);
+      value = s_add_copyright_parameter(r, value);
       W_L(" src=\"");
       W_V(value);
       W_L("\"");
@@ -3441,6 +3445,72 @@ s_jhtml_newline_mark(void *pdoc, Node *UNUSED(node))
   W_NLCODE();
   return jhtml->out;
 }
+
+static char *
+s_add_copyright_parameter(request_rec *r, char *value)
+{
+  request_rec *sub_req = NULL;
+  apr_pool_t *pool;
+  int rr_status;
+  char *content_type = NULL;
+  apr_table_t *headers_out = NULL;
+
+  DBG(r, "REQ[%X] start s_add_copyright_parameter", TO_ADDR(r));
+  apr_pool_create(&pool, r->pool);
+
+  if (chxj_starts_with(value, "http:") || chxj_starts_with(value, "https:")) {
+    apr_table_t *response;
+    int   response_code = 0;
+    response = chxj_serf_head(r, pool, value, &response_code);
+    if (response_code != HTTP_OK) {
+      DBG(r, "REQ[%X] end s_add_copyright_parameter (serf_req->status:[%d])", TO_ADDR(r), response_code);
+      return value;
+    }
+    content_type = (char *)apr_table_get(response, "Content-Type");
+    headers_out = response;
+  }
+  else {
+    //  sub_req = ap_sub_req_method_uri("GET", value, r, r->output_filters);
+    sub_req = ap_sub_req_lookup_uri(value, r, r->output_filters);
+    if (sub_req->status != HTTP_OK) {
+      DBG(r, "REQ[%X] end s_add_copyright_parameter (sub_req->status:[%d])", TO_ADDR(r), sub_req->status);
+      ap_destroy_sub_req(sub_req);
+      return value;
+    }
+    sub_req->header_only = 1;
+    sub_req->main = NULL;
+    rr_status = ap_run_sub_req(sub_req);
+    DBG(r, "REQ[%X] sub_req4:ContentType:[%s]", TO_ADDR(r), sub_req->content_type);
+    DBG(r, "REQ[%X] rr_status:[%d]", TO_ADDR(r), rr_status);
+    content_type = apr_pstrdup(pool, sub_req->content_type);
+    headers_out = sub_req->headers_out;
+  }
+  if (headers_out && apr_table_get(headers_out, "x-jphone-copyright")) {
+    if (content_type && strncasecmp("image/jpeg", content_type, 10) == 0) {
+      if (strchr(value, '?')) {
+        value = apr_psprintf(pool, "%s&_chxj_copy=.jpz", value);
+      }
+      else {
+        value = apr_psprintf(pool, "%s?_chxj_copy=.jpz", value);
+      }
+    }
+    else if (content_type && strncasecmp("image/png", content_type, 9) == 0) {
+      if (strchr(value, '?')) {
+        value = apr_psprintf(pool, "%s&_chxj_copy=.pnz", value);
+      }
+      else {
+        value = apr_psprintf(pool, "%s?_chxj_copy=.pnz", value);
+      }
+    }
+  }
+  if (sub_req) {
+    ap_destroy_sub_req(sub_req);
+  }
+  DBG(r, "REQ[%X] end s_add_copyright_parameter(result:[%s]", TO_ADDR(r), value);
+  return value;
+}
+
+
 /*
  * vim:ts=2 et
  */
index 3512601..67582cd 100644 (file)
@@ -61,6 +61,8 @@ char *default_chxj_serf_get(request_rec *r, apr_pool_t *ppool, const char *url_p
 char *(*chxj_serf_get)(request_rec *r, apr_pool_t *ppool, const char *url_path, int set_headers_flag, apr_size_t *response_len) = default_chxj_serf_get;
 char *default_chxj_serf_post(request_rec *r, apr_pool_t *ppool, const char *url_path, char *post_data, apr_size_t post_data_len, int set_headers_flag, apr_size_t *response_len, int *response_code);
 char *(*chxj_serf_post)(request_rec *r, apr_pool_t *ppool, const char *url_path, char *post_data, apr_size_t post_data_len, int set_headers_flag, apr_size_t *response_len, int *response_code) = default_chxj_serf_post;
+apr_table_t *default_chxj_serf_head(request_rec *r, apr_pool_t *ppool, const char *url_path, int *response_code);
+apr_table_t *(*chxj_serf_head)(request_rec *r, apr_pool_t *ppool, const char *url_path, int *response_code) = default_chxj_serf_head;
 
 
 void
@@ -496,6 +498,118 @@ default_chxj_serf_post(request_rec *r, apr_pool_t *ppool, const char *url_path,
   DBG(r, "REQ:[%X] end chxj_serf_post()", (unsigned int)(apr_size_t)r);
   return ret;
 }
+
+
+apr_table_t *
+default_chxj_serf_head(request_rec *r, apr_pool_t *ppool, const char *url_path, int *response_code)
+{
+  apr_pool_t *pool;
+  apr_uri_t url;
+  apr_status_t rv;
+  apr_sockaddr_t *address = NULL;
+
+  serf_context_t *context;
+  serf_connection_t *connection;
+
+  app_ctx_t app_ctx;
+  handler_ctx_t handler_ctx;
+  char *ret;
+
+  DBG(r, "REQ:[%X] start chxj_serf_head()", (unsigned int)(apr_size_t)r);
+
+
+  s_init(ppool, &pool);
+
+  apr_uri_parse(pool, url_path, &url);
+  if (!url.port) {
+    url.port = apr_uri_port_of_scheme(url.scheme);
+  }
+  if (!url.port) {
+    url.port = 80;
+  }
+  if (!url.path) {
+    url.path = "/";
+  }
+  if (!url.hostname) {
+    url.hostname = "localhost";
+  }
+  if (url.query) {
+    url.path = apr_psprintf(pool, "%s?%s", url.path, url.query);
+  }
+
+  rv = apr_sockaddr_info_get(&address, url.hostname, APR_UNSPEC, url.port, 0, pool);
+  if (rv != APR_SUCCESS) {
+    char buf[256];
+    ERR(r, "apr_sockaddr_info_get() failed: rv:[%d|%s]", rv, apr_strerror(rv, buf, 256));
+    return NULL;
+  }
+  memset(&app_ctx, 0, sizeof(app_ctx_t));
+
+  app_ctx.bkt_alloc = serf_bucket_allocator_create(pool, NULL, NULL);
+  if (strcasecmp(url.scheme, "https") == 0) {
+    app_ctx.ssl_flag = 1;
+  }
+
+  context = serf_context_create(pool);
+  connection = serf_connection_create(context, address, s_connection_setup, &app_ctx, s_connection_closed, &app_ctx, pool);
+
+  memset(&handler_ctx, 0, sizeof(handler_ctx_t));
+  handler_ctx.requests_outstanding = 0;
+  handler_ctx.host = url.hostinfo;
+  /*========================================================================================================*/
+  /* XXX Maybe, libserf doesn't support the HEAD request. Because the part body is waited for with polling. */
+  /*========================================================================================================*/
+  handler_ctx.method = "GET";
+  handler_ctx.path = url.path;
+  handler_ctx.user_agent = (char *)apr_table_get(r->headers_in, CHXJ_HTTP_USER_AGENT);
+  if (! handler_ctx.user_agent) {
+    handler_ctx.user_agent = (char *)apr_table_get(r->headers_in, HTTP_USER_AGENT);
+  }
+  handler_ctx.post_data     = NULL;
+  handler_ctx.post_data_len = 0;
+
+  handler_ctx.acceptor     = s_accept_response;
+  handler_ctx.acceptor_ctx = &app_ctx;
+  handler_ctx.handler      = s_handle_response;
+  handler_ctx.pool         = pool;
+  handler_ctx.r            = r;
+  handler_ctx.response_len = 0;
+  handler_ctx.response     = NULL;
+
+  serf_connection_request_create(connection, s_setup_request, &handler_ctx);
+
+  while (1) {
+    rv = serf_context_run(context, SERF_DURATION_FOREVER, pool);
+    if (APR_STATUS_IS_TIMEUP(rv))
+      continue;
+    if (rv) {
+      char buf[200];
+      ERR(r, "Error running context: (%d) %s\n", rv, apr_strerror(rv, buf, sizeof(buf)));
+      break;
+    }
+    if (!apr_atomic_read32(&handler_ctx.requests_outstanding)) {
+      if (handler_ctx.rv != APR_SUCCESS) {
+        char buf[200];
+        ERR(r, "Error running context: (%d) %s\n", handler_ctx.rv, apr_strerror(handler_ctx.rv, buf, sizeof(buf)));
+      }
+      break;
+    }
+  }
+
+  DBG(r, "end of serf request");
+  DBG(r, "response_code:[%d]", handler_ctx.response_code);
+  DBG(r, "response:[%s][%" APR_SIZE_T_FMT "]", handler_ctx.response, handler_ctx.response_len);
+  serf_connection_close(connection);
+  if (handler_ctx.response) {
+    ret = apr_pstrdup(ppool, handler_ctx.response);
+  }
+  else {
+    ret = apr_pstrdup(ppool, "");
+  }
+  *response_code = handler_ctx.response_code;
+  DBG(r, "REQ:[%X] end chxj_serf_post()", (unsigned int)(apr_size_t)r);
+  return handler_ctx.headers_out;
+}
 /*
  * vim:ts=2 et
  */
index 10aa224..b4f1f22 100644 (file)
@@ -1560,6 +1560,7 @@ chxj_translate_name(request_rec *r)
   DBG(r, "REQ[%X] =======================================================================", (unsigned int)(apr_size_t)r);
   DBG(r, "REQ[%X] ", (unsigned int)(apr_size_t)r);
   DBG(r, "REQ[%X] START REQUEST (uri:[%s] args:[%s])", (unsigned int)(apr_size_t)r, r->unparsed_uri, r->args ? r->args : "");
+  DBG(r, "REQ[%X] METHOD [%s]", TO_ADDR(r), r->method);
   DBG(r, "REQ[%X] ", (unsigned int)(apr_size_t)r);
   DBG(r, "REQ[%X] =======================================================================", (unsigned int)(apr_size_t)r);
 #if 0