Пример #1
0
ci_off_t ci_http_content_length(ci_request_t * req)
{
     ci_headers_list_t *heads;
     const char *val;
     ci_off_t res = 0;
     char *e;
     if (!(heads =  ci_http_response_headers(req))) {
          /*Then maybe is a reqmod reauest, try to get request headers */
          if (!(heads = ci_http_request_headers(req)))
               return 0;
     }
     if (!(val = ci_headers_value(heads, "Content-Length")))
          return -1;
 
     errno = 0;
     res = ci_strto_off_t(val, &e, 10);
     if (errno == ERANGE && (res == CI_STRTO_OFF_T_MAX || res == CI_STRTO_OFF_T_MIN)) {
         ci_debug_printf(4, "Content-Length: overflow\n");
         return -2;
     }
     if (val == e) {
         ci_debug_printf(4, "Content-Length: not valid value: '%s' \n", val);
         return -2;
     }
     return res;
}
Пример #2
0
int ci_http_request_reset_headers(ci_request_t * req)
{
     ci_headers_list_t *heads;
     if (!(heads = ci_http_request_headers(req)))
          return 0;
     ci_headers_reset(heads);
     return 1;
}
Пример #3
0
int ci_http_request_remove_header(ci_request_t * req, const char *header)
{
     ci_headers_list_t *heads;
     if(req->packed)   /*Not in edit mode*/
	  return 0;
     if (!(heads = ci_http_request_headers(req)))
          return 0;
     return ci_headers_remove(heads, header);
}
Пример #4
0
const char *ci_http_request_add_header(ci_request_t * req, const char *header)
{
     ci_headers_list_t *heads;
     if(req->packed)   /*Not in edit mode*/
	  return NULL;
     if (!(heads = ci_http_request_headers(req)))
          return NULL;
     return ci_headers_add(heads, header);
}
Пример #5
0
const char *ci_http_request_get_header(ci_request_t * req, const char *head_name)
{
     ci_headers_list_t *heads;
     const char *val;
     if (!(heads = ci_http_request_headers(req)))
          return NULL;
     if (!(val = ci_headers_value(heads, head_name)))
          return NULL;
     return val;
}
Пример #6
0
const char *ci_http_request(ci_request_t * req)
{
     ci_headers_list_t *heads;
     if (!(heads = ci_http_request_headers(req)))
          return NULL;

     if (!heads->used)
        return NULL;

     return heads->headers[0];
}
Пример #7
0
int ci_http_request_url(ci_request_t * req, char *buf, int buf_size)
{
   ci_headers_list_t *heads;
   const char *str, *host;
   int i, bytes; 
   /*The request must have the form:
        GET url HTTP/X.X 
   */
    if (!(heads = ci_http_request_headers(req)))
          return 0;

    if (!heads->used)
        return 0;

    str = heads->headers[0];

    if ((str = strchr(str, ' ')) == NULL) { /*Ignore method i*/
          return 0;
     }
     while (*str == ' ') /*ignore spaces*/
          str++;

     bytes = 0;
     if (*str == '/' && (host = ci_headers_value(heads,"Host"))) {
         /*Looks like a transparent proxy, we do not know the protocol lets try
           to preserve the major part of the url....
          */
         for (i=0; (i < buf_size-1) && !header_end(host[i]) && !isspace(host[i]); i++) {
             buf[i] = host[i]; 
         }
         buf += i;
         buf_size -= i;
         bytes = i;
     }

     /*copy the url...*/
     for (i=0; (i < buf_size-1) && !header_end(str[i]) && !isspace(str[i]) && str[i] != '?'; i++) {
          buf[i] = str[i]; 
     }
     buf[i] = '\0';
     bytes += i;
     return bytes;
}
Пример #8
0
void print_headers(ci_request_t * req)
{
     int type;
     ci_headers_list_t *headers;
     ci_debug_printf(1, "\nICAP HEADERS:\n");
     ci_headers_iterate(req->response_header, NULL, printhead);
     ci_debug_printf(1, "\n");

     if ((headers =  ci_http_response_headers(req)) == NULL) {
          headers = ci_http_request_headers(req);
          type = ICAP_REQMOD;
     }
     else
          type = ICAP_RESPMOD;

     if (headers) {
          ci_debug_printf(1, "%s HEADERS:\n", ci_method_string(type));
          ci_headers_iterate(headers, NULL, printhead);
          ci_debug_printf(1, "\n");
     }
}
Пример #9
0
int url_check_check_preview(char *preview_data, int preview_data_len,
                            ci_request_t * req)
{
     ci_headers_list_t *req_header;
     struct url_check_data *uc = ci_service_data(req);
     struct http_info httpinf;
     struct profile *profile;
     int pass = DB_PASS;

     if ((req_header = ci_http_request_headers(req)) == NULL) /*It is not possible but who knows ..... */
          return CI_ERROR;

     if (!get_http_info(req, req_header, &httpinf)) /*Unknown method or something else...*/
	 return CI_MOD_ALLOW204;

     ci_debug_printf(9, "URL  to host %s\n", httpinf.site);
     ci_debug_printf(9, "URL  page %s\n", httpinf.url);

     profile = profile_select(req);

     if (!profile) {
          ci_debug_printf(1, "No Profile configured! Allowing the request...\n");
	  return CI_MOD_ALLOW204;
     }

     if ((pass=profile_access(profile, &httpinf)) == DB_ERROR) {
          ci_debug_printf(1,"Error searching in profile! Allow the request\n");
	  return CI_MOD_ALLOW204;;
     }


     if (pass == DB_BLOCK) {
          /*The URL is not a good one so.... */
          ci_debug_printf(9, "Oh!!! we are going to deny this site.....\n");

          uc->denied = 1;
          uc->body = ci_cached_file_new(strlen(error_message) + 10);
          ci_http_response_create(req, 1, 1); /*Build the responce headers */

          ci_http_response_add_header(req, "HTTP/1.0 403 Forbidden"); /*Send an 403 Forbidden http responce to web client */
          ci_http_response_add_header(req, "Server: C-ICAP");
          ci_http_response_add_header(req, "Content-Type: text/html");
          ci_http_response_add_header(req, "Content-Language: en");
          ci_http_response_add_header(req, "Connection: close");

          ci_cached_file_write(uc->body, error_message, strlen(error_message),
                               1);

     }
     else {
          /*if we are inside preview negotiation or client allow204 responces oudsite of preview then */
          if (preview_data || ci_req_allow204(req))
               return CI_MOD_ALLOW204;

          /*
             icap client does not support preview of data in reqmod requests neither 204 responces outside preview
             so we need to read all the body if exists and send it back to client.
             Allocate a new body for it 
           */
          if (ci_req_hasbody(req)) {
               int clen = ci_http_content_length(req) + 100;
               uc->body = ci_cached_file_new(clen);
          }

     }

     unlock_data(req);
     return CI_MOD_CONTINUE;
}
Пример #10
0
void free_http_req_header(ci_request_t *req, void *param)
{
    ci_headers_list_t *heads;
    heads = ci_http_request_headers(req);
    release_header_value(heads, param);
}
Пример #11
0
void *get_http_req_header(ci_request_t *req, char *param)
{
    ci_headers_list_t *heads;
    heads = ci_http_request_headers(req);
    return (void *)get_header(heads, param);
}
Пример #12
0
/**
 * initialize ICAP Request.<br>
 * prev = recv Request<br>
 * next = java_check_preview_handler(char * preview_data, int preview_data_len, ci_request_t * req)<br>
 *
 * @see java_check_preview_handler(char * preview_data, int preview_data_len, ci_request_t * req)
 * @param req a pointer of request data.
 * @return service_data instance if OK else returns NULL(pass)
 */
void * java_init_request_data(ci_request_t * req) {
    const int REQ_TYPE = ci_req_type(req);
    const char * METHOD_TYPE = ci_method_string(REQ_TYPE);//Don't free!

    ci_headers_list_t *hdrs = NULL;

    //identify reqmod or respmod or else
    if (REQ_TYPE == ICAP_REQMOD) {
        hdrs = ci_http_request_headers(req);
    } else if (REQ_TYPE == ICAP_RESPMOD) {
        hdrs = ci_http_response_headers(req);
    } else if (REQ_TYPE == ICAP_OPTIONS){
        cij_debug_printf(CIJ_INFO_LEVEL, "ICAP OPTIONS comes. ignoring...");
        return NULL;//pass
    } else {
        //UNKNOWN ICAP METHOD
        cij_debug_printf(CIJ_INFO_LEVEL, "INVALID ICAP METHOD (NO. %d) has come. ignoring...", REQ_TYPE);
        return NULL;//pass
    }

    //Create service_data
    jServiceData_t * jServiceData = NULL;
    jServiceData = (jServiceData_t *)malloc(sizeof(jServiceData_t));
    if (jServiceData == NULL) {
        cij_debug_printf(CIJ_ERROR_LEVEL, "Unable to allocate memory for jServiceData_t !");
        cij_debug_printf(CIJ_ERROR_LEVEL, "Dropping request...");
        return NULL;
    }

    //Get Java environment from mod_name
    const char * mod_name = (req->current_service_mod)->mod_name;
    jData_t * jdata = (jData_t *)ci_dyn_array_search(environments, mod_name);
    if (jdata == NULL) {
        cij_debug_printf(CIJ_ERROR_LEVEL, "Invalid mod_name %s is not in environments array.", mod_name);
        free(jServiceData);
        return NULL;
    }

    //Attach service instance to JVM
    jServiceData->jdata = jdata;
    JNIEnv * jni = jdata->jni;

    //Create new HTTP headers array for java
    jclass jClass_String = (*jni)->FindClass(jni, "java/lang/String");
    jobjectArray jHeaders = (*jni)->NewObjectArray(jni, hdrs->used, jClass_String, NULL);
    if (jHeaders == NULL) {
        cij_debug_printf(CIJ_ERROR_LEVEL, "Could not allocate memory for http headers. ignoring...");
        return NULL;
    }
    (*jni)->DeleteLocalRef(jni, jClass_String);
    int i;
    for(i=0;i<hdrs->used;i++) {
        jstring buf = (*jni)->NewStringUTF(jni, hdrs->headers[i]);
        if (buf == NULL) {
            cij_debug_printf(CIJ_ERROR_LEVEL, "Could not allocate memory for http headers string. ignoring...");
            return NULL;
        }
        (*jni)->SetObjectArrayElement(jni, jHeaders, i, buf);
        (*jni)->DeleteLocalRef(jni, buf);
    }

    //Create instance.
    jobject jInstance = (*jni)->NewObject(jni, jdata->jIcapClass, jdata->jServiceConstructor,METHOD_TYPE,jHeaders);
    (*jni)->DeleteLocalRef(jni, jHeaders);
    if (jInstance == NULL) {
        cij_debug_printf(CIJ_ERROR_LEVEL, "Could not create instance of the class '%s'. Method='%s'. ignoring...", mod_name, METHOD_TYPE);
        return NULL;
    }
    jServiceData->instance = jInstance;//+REF
    ci_membuf_t * buffer = ci_membuf_new();
    if (buffer == NULL) {
        cij_debug_printf(CIJ_ERROR_LEVEL, "Could not allocate memory for http body ignoring...");
        return NULL;
    }
    return (void *)jServiceData;
}