Пример #1
0
static char *
parse_req (char *line, char **method, char **protocol)
{
  char *req = NULL, *request = NULL, *proto = NULL, *dreq = NULL;
  const char *meth;
  ptrdiff_t rlen;

  meth = extract_method (line);

  /* couldn't find a method, so use the whole request line */
  if (meth == NULL) {
    request = xstrdup (line);
  }
  /* method found, attempt to parse request */
  else {
    req = line + strlen (meth);
    if ((proto = strstr (line, " HTTP/1.0")) == NULL &&
        (proto = strstr (line, " HTTP/1.1")) == NULL) {
      return alloc_string ("-");
    }

    req++;
    if ((rlen = proto - req) <= 0)
      return alloc_string ("-");

    request = xmalloc (rlen + 1);
    strncpy (request, req, rlen);
    request[rlen] = 0;

    if (conf.append_method)
      (*method) = strtoupper (xstrdup (meth));

    if (conf.append_protocol)
      (*protocol) = strtoupper (xstrdup (++proto));
  }

  if ((dreq = decode_url (request)) && dreq != '\0') {
    free (request);
    return dreq;
  }

  return request;
}
void client_handler::handle(const epoll_event &e) {
    if (!(e.events & EPOLLOUT) || (e.events & EPOLLIN) || !output_buffer.empty()) {
        Log::d("Client handler: " + eetostr(e));
    }
    if (e.events & EPOLLOUT) {
        if (serv->write_chunk(this, output_buffer) && message_type != HTTPS_MODE) {
            Log::d("Finished resending host response to client");
            if (message_type == PRE_HTTPS_MODE) {
                message_type = HTTPS_MODE;

                std::string hostname;
                extract_header(input_buffer.string_data(), input_buffer.length(), "Host", hostname);
                input_buffer.clear();
                output_buffer.clear();
                serv->modify_handler(this, EPOLLIN | EPOLLOUT);
                resolve_host_ip(hostname, EPOLLIN | EPOLLOUT);
            } else {
                input_buffer.clear();
                message_len = -1;
                message_type = NOT_EVALUATED;
                serv->modify_handler(this, EPOLLIN);
            }
        }
    }

    if (e.events & EPOLLIN) {
        if (read_message(this, input_buffer) && message_type != HTTPS_MODE) {
            std::string data = input_buffer.string_data();

            serv->modify_handler(this, 0);

            std::string hostname;
            if (extract_header(data, (int) data.length(), "Host", hostname)) {
                if (message_type == WITHOUT_BODY && extract_method(data) == "CONNECT") {
                    //output_buffer.set("HTTP/1.0 404 Fail\r\nProxy-agent: BotHQ-Agent/1.2\r\n\r\n");
                    output_buffer.set("HTTP/1.0 200 OK\r\nProxy-agent: BotHQ-Agent/1.2\r\n\r\n");
                    serv->modify_handler(this, EPOLLOUT);
                    message_type = PRE_HTTPS_MODE;
                    Log::d("entering HTTPS_MODE");
                } else {
                    if (message_type == WITHOUT_BODY && extract_method(data) == "GET") {
                        // modifying status line
                        size_t from = data.find(hostname) + hostname.length();

                        Log::d("from is " + inttostr(from) + ", hostname is " + hostname);

                        data = "GET " +
                               input_buffer.string_data().substr(from, input_buffer.string_data().length() - from);
                        input_buffer.clear();
                        input_buffer.put(data.c_str(), data.length());

                        Log::d("new query is: \"\n" + input_buffer.string_data() + "\"");
                        Log::d("First line is \"" +
                               input_buffer.string_data().substr(0, input_buffer.string_data().find("\r\n")) + "\"");
                    }

                    resolve_host_ip(hostname, EPOLLOUT);
                }
            } else {
                //TODO: send correspond answer
                Log::fatal("Client headers do not contain 'host' header");
                //exit(-1);
            }
        }
    }
}
Пример #3
0
static int
parse_format (GLogItem * glog, const char *fmt, const char *date_format,
              char *str)
{
  const char *p;
  double serve_secs;
  int special = 0;
  struct tm tm;
  unsigned long long bandw, serve_time;

  if (str == NULL || *str == '\0')
    return 1;

  memset (&tm, 0, sizeof (tm));

  /* iterate over the log format */
  for (p = fmt; *p; p++) {
    if (*p == '%') {
      special++;
      continue;
    }
    if (special && *p != '\0') {
      char *pch, *sEnd, *bEnd, *tkn = NULL, *end = NULL;
      errno = 0;
      bandw = 0;
      serve_time = 0;
      serve_secs = 0;

      switch (*p) {
         /* date */
       case 'd':
         if (glog->date)
           return 1;
         /* parse date format including dates containing spaces,
          * i.e., syslog date format (Jul 15 20:10:56) */
         tkn = parse_string (&str, p[1], count_matches (date_format, ' ') + 1);
         if (tkn == NULL)
           return 1;
         end = strptime (tkn, date_format, &tm);
         if (end == NULL || *end != '\0') {
           free (tkn);
           return 1;
         }
         glog->date = tkn;
         break;
         /* remote hostname (IP only) */
       case 'h':
         if (glog->host)
           return 1;
         tkn = parse_string (&str, p[1], 1);
         if (tkn == NULL)
           return 1;
         if (invalid_ipaddr (tkn)) {
           free (tkn);
           return 1;
         }
         glog->host = tkn;
         break;
         /* request method */
       case 'm':
         if (glog->method)
           return 1;
         tkn = parse_string (&str, p[1], 1);
         if (tkn == NULL)
           return 1;
         if (!extract_method (tkn)) {
           free (tkn);
           return 1;
         }
         glog->method = tkn;
         break;
         /* request not including method or protocol */
       case 'U':
         if (glog->req)
           return 1;
         tkn = parse_string (&str, p[1], 1);
         if (tkn == NULL || *tkn == '\0')
           return 1;
         if ((glog->req = decode_url (tkn)) == NULL)
           return 1;
         free (tkn);
         break;
         /* request protocol */
       case 'H':
         if (glog->protocol)
           return 1;
         tkn = parse_string (&str, p[1], 1);
         if (tkn == NULL)
           return 1;
         if (invalid_protocol (tkn)) {
           free (tkn);
           return 1;
         }
         glog->protocol = tkn;
         break;
         /* request, including method + protocol */
       case 'r':
         if (glog->req)
           return 1;
         tkn = parse_string (&str, p[1], 1);
         if (tkn == NULL)
           return 1;
         glog->req = parse_req (tkn, &glog->method, &glog->protocol);
         free (tkn);
         break;
         /* Status Code */
       case 's':
         if (glog->status)
           return 1;
         tkn = parse_string (&str, p[1], 1);
         if (tkn == NULL)
           return 1;
         strtol (tkn, &sEnd, 10);
         if (tkn == sEnd || *sEnd != '\0' || errno == ERANGE) {
           free (tkn);
           return 1;
         }
         glog->status = tkn;
         break;
         /* size of response in bytes - excluding HTTP headers */
       case 'b':
         if (glog->resp_size)
           return 1;
         tkn = parse_string (&str, p[1], 1);
         if (tkn == NULL)
           return 1;
         bandw = strtol (tkn, &bEnd, 10);
         if (tkn == bEnd || *bEnd != '\0' || errno == ERANGE)
           bandw = 0;
         glog->resp_size = bandw;
         conf.bandwidth = 1;
         free (tkn);
         break;
         /* referrer */
       case 'R':
         if (glog->ref)
           return 1;
         tkn = parse_string (&str, p[1], 1);
         if (tkn == NULL)
           tkn = alloc_string ("-");
         if (tkn != NULL && *tkn == '\0') {
           free (tkn);
           tkn = alloc_string ("-");
         }
         if (strcmp (tkn, "-") != 0)
           extract_referer_site (tkn, glog->site);
         glog->ref = tkn;
         break;
         /* user agent */
       case 'u':
         if (glog->agent)
           return 1;
         tkn = parse_string (&str, p[1], 1);
         if (tkn != NULL && *tkn != '\0') {
           /* Make sure the user agent is decoded (i.e.: CloudFront)
            * and replace all '+' with ' ' (i.e.: w3c) */
           glog->agent = char_replace (decode_url (tkn), '+', ' ');
           free (tkn);
           break;
         } else if (tkn != NULL && *tkn == '\0') {
           free (tkn);
           tkn = alloc_string ("-");
         }
         /* must be null */
         else {
           tkn = alloc_string ("-");
         }
         glog->agent = tkn;
         break;
         /* time taken to serve the request, in seconds */
       case 'T':
         if (glog->serve_time)
           return 1;
         /* ignore seconds if we have microseconds */
         if (strstr (fmt, "%D") != NULL)
           break;

         tkn = parse_string (&str, p[1], 1);
         if (tkn == NULL)
           return 1;
         if (strchr (tkn, '.') != NULL)
           serve_secs = strtod (tkn, &bEnd);
         else
           serve_secs = strtoull (tkn, &bEnd, 10);

         if (tkn == bEnd || *bEnd != '\0' || errno == ERANGE)
           serve_secs = 0;
         /* convert it to microseconds */
         if (serve_secs > 0)
           glog->serve_time = serve_secs * SECS;
         else
           glog->serve_time = 0;
         conf.serve_usecs = 1;
         free (tkn);
         break;
         /* time taken to serve the request, in microseconds */
       case 'D':
         if (glog->serve_time)
           return 1;
         tkn = parse_string (&str, p[1], 1);
         if (tkn == NULL)
           return 1;
         serve_time = strtoull (tkn, &bEnd, 10);
         if (tkn == bEnd || *bEnd != '\0' || errno == ERANGE)
           serve_time = 0;
         glog->serve_time = serve_time;
         conf.serve_usecs = 1;
         free (tkn);
         break;
         /* everything else skip it */
       default:
         if ((pch = strchr (str, p[1])) != NULL)
           str += pch - str;
      }
      if ((str == NULL) || (*str == '\0'))
        return 0;
      special = 0;
    } else if (special && isspace (p[0])) {
      return 1;
    } else
      str++;
  }
  return 0;
}
Пример #4
0
int main(int argc, char** argv) {
  bool extract, train;
  int width, height,
      max_horizontal_steps,
      max_vertical_steps,
      video_source;
  std::string output_file;
  std::string model_file;
  std::string positive_source_directory;
  std::string negative_source_directory;

  try {
    namespace po=boost::program_options;
    po::options_description desc("Options");
    desc.add_options()
    ("help,h", "Print help messages")
    ("extract,e", po::value<bool>(&extract)->default_value(true), "Specify whether to extract features")
    ("train,t", po::value<bool>(&train)->default_value(false), "Specify whether to train")
    ("camera,c", po::value<int>(&video_source)->default_value(0), "Specify camera to retrieve test feed")
    ("width,w", po::value<int>(&width)->required(), "Specify train window width")
    ("height,h", po::value<int>(&height)->required(), "Specify train window height")
    ("max_horizontal_steps,mh", po::value<int>(&max_horizontal_steps)->default_value(0), "Specify max horizontal steps the window can take")
    ("max_vertical_steps,mh", po::value<int>(&max_vertical_steps)->default_value(0), "Specify max vertical steps the window can take")
    ("positive,p", po::value<std::string>(&positive_source_directory)->default_value(boost::filesystem::current_path().string<std::string>()+"/positive"), "Specify positive video files directory")
    ("negative,n", po::value<std::string>(&negative_source_directory)->default_value(boost::filesystem::current_path().string<std::string>()+"/negative"), "Specify negative video files direcotry")
    ("output,o", po::value<std::string>(&output_file)->default_value(boost::filesystem::current_path().string<std::string>()+"/features.dump"), "Specify an features file for save/load")
    ("model,m", po::value<std::string>(&model_file)->default_value(boost::filesystem::current_path().string<std::string>()+"/result.model"), "Specify an model file for save/load");

    po::variables_map vm;
    po::store(po::command_line_parser(argc,argv).options(desc).run(), vm);

    if (vm.count("help")) {
      std::cout << "Usage: " << argv[0] << " [options]" << std::endl;
      std::cout << desc;
      return 0;
    }

    po::notify(vm);
  } catch(std::exception& e) {
    std::cerr << "Error: " << e.what() << std::endl;
    return 1;
  } catch(...) {
    std::cerr << "Exception of unknown type!" << std::endl;
    return 1;
  }

  const cv::Size window_size=cv::Size(width, height);

  std::vector<maav::Features> features_collection;
  std::vector<unsigned int> divider;

  maav::HOGExtractor extractor(window_size);

  if(extract) {
    maav::FeatureExtractMethod extract_method(extractor, features_collection);
    boost::function<void (const cv::Mat &)> f=boost::ref(extract_method);

    boost::filesystem::path positive_dir(positive_source_directory);
    if(boost::filesystem::is_directory(positive_dir)) {
      boost::filesystem::directory_iterator dir_iter(positive_dir), eod;

      unsigned int counter=0;
      BOOST_FOREACH(boost::filesystem::path const &file_path, std::make_pair(dir_iter, eod)) {
        maav::LoadEachFrameFromFile(file_path.string<std::string>(), f);
        for(unsigned int i=0;i<(features_collection.size()-divider.size());i++) {
          divider.push_back(counter);
        }
        counter++;
      }
    }

    boost::filesystem::path negative_dir(positive_source_directory);
    if(boost::filesystem::is_directory(negative_dir)) {
      boost::filesystem::directory_iterator dir_iter(negative_dir), eod;

      BOOST_FOREACH(boost::filesystem::path const &file_path, std::make_pair(dir_iter, eod)) {
        maav::LoadEachFrameFromFile(file_path.string<std::string>(), f);
      }
    }