Example #1
0
int fmt_http_res_head_o(ci_request_t *req, char *buf,int len, char *param)
{
  char *s = NULL;
  int i;
  ci_headers_list_t *http_resp_headers;

  if (!len)
     return 0;

  if (!param || param[0] == '\0') {
      http_resp_headers = ci_http_response_headers(req);
      if (http_resp_headers && http_resp_headers->used)
          s = http_resp_headers->headers[0];
  } else {
      s = ci_http_response_get_header(req, param);
  }

  if (s) {
     for (i=0;i<len && *s!= '\0' && *s != '\r' && *s!='\n'; i++,s++)
        buf[i] = *s;
     return i;
  } else {
     *buf = '-';
     return 1;
  }
}
/*This function will be executed when a new request for search_record service arrives. This function will
  initialize the required structures and data to serve the request.
 */
void *search_record_init_request_data(ci_request_t * req)
{
    struct SearchRecord *search_record_data;
	char full_url[URL_LEN];
	char ip[IP_LEN];
	const char* content_type=ci_http_response_get_header(req,"Content-Type");
	int url_state = ci_http_request_full_url(req,full_url,URL_LEN);
	const char* ip_state = get_client_ip(req,ip);
	if((url_state == 0) || (ip_state == NULL))
	{
		ci_debug_printf(2,"could not get full url or client ip\n");
		return NULL;
	}
	if((content_type == NULL) || (strncmp(content_type,"text/html",9) == 0))
	{
		search_record_data = malloc(sizeof(struct SearchRecord));
	    if (!search_record_data) 
		{
			ci_debug_printf(1, "Memory allocation failed inside search_record_init_request_data!\n");
			return NULL;
		}
		strcpy(search_record_data->full_url,full_url);
		strcpy(search_record_data->ip,ip);
		return search_record_data;
	}
	else
		return NULL;
}
static void demonstrateDetection(image_session_t *mySession)
{
struct stat stat_buf;
classify_req_data_t *data = ci_service_data(mySession->req);
char imageFILENAME[CI_MAX_PATH + 1];

	// Go back to a file so we can save a modified version
	memBodyToDiskBody(mySession->req);

	ci_http_response_remove_header(mySession->req, "Content-Length");
	data->disk_body->readpos = 0;
	close(data->disk_body->fd);
	unlink(data->disk_body->filename);
	if(strstr(ci_http_response_get_header(mySession->req, "Content-Type"), "jpeg"))
	{
		snprintf(imageFILENAME, CI_MAX_PATH, "%s.jpg", data->disk_body->filename);
		imageFILENAME[CI_MAX_PATH] = '\0';
		cvSaveImage(imageFILENAME, mySession->origImage, jpeg_p);
	}
	else
	{
		snprintf(imageFILENAME, CI_MAX_PATH, "%s.png", data->disk_body->filename);
		imageFILENAME[CI_MAX_PATH] = '\0';
		cvSaveImage(imageFILENAME, mySession->origImage, png_p);
		ci_http_response_remove_header(mySession->req, "Content-Type");
		ci_http_response_add_header(mySession->req, "Content-Type: image/png");
	}
	snprintf(data->disk_body->filename, CI_FILENAME_LEN + 1, "%s", imageFILENAME);
	data->disk_body->filename[CI_FILENAME_LEN] = '\0';
	data->disk_body->fd = open(data->disk_body->filename, O_RDWR | O_EXCL, F_PERM);
	if(fstat(data->disk_body->fd, &stat_buf) == 0)
		data->disk_body->bytes_in = data->disk_body->endpos = stat_buf.st_size;
	else
		data->disk_body->bytes_in = data->disk_body->endpos = 0;
	// Make new content length header
	ci_http_response_remove_header(mySession->req, "Content-Length");
	snprintf(imageFILENAME, CI_MAX_PATH, "Content-Length: %ld", (long) data->disk_body->endpos);
	imageFILENAME[CI_MAX_PATH] = '\0';
	ci_http_response_add_header(mySession->req, imageFILENAME);
}