void MiniWebServer::process() {
    client_ = server_.available();
    if (!client_.connected() || !client_.available()) {
        return;
    }

    boolean is_complete = get_line(buffer, sizeof(buffer));
    if (!buffer[0]) {
        return;
    }
#if DEBUG
    Serial << F("TWS:New request: ");
    Serial.println(buffer);
#endif
    if (!is_complete) {
        // The requested path is too long.
        send_error_code(414);
        client_.stop();
        return;
    }

    char* request_type_str = get_field(buffer, 0);
    request_type_ = UNKNOWN_REQUEST;
    if (!strcmp("GET", request_type_str)) {
        request_type_ = GET;
    } else if (!strcmp("POST", request_type_str)) {
        request_type_ = POST;
    } else if (!strcmp("PUT", request_type_str)) {
        request_type_ = PUT;
    }
    path_ = get_field(buffer, 1);

    // Process the headers.
    if (!process_headers()) {
        // Malformed header line.
        send_error_code(417);
        client_.stop();
    }
    // Header processing finished. Identify the handler to call.

    boolean should_close = true;
    boolean found = false;
    for (int i = 0; handlers_[i].path; i++) {
        int len = strlen(handlers_[i].path);
        boolean exact_match = !strcmp(path_, handlers_[i].path);
        boolean regex_match = false;
        if (handlers_[i].path[len - 1] == '*') {
            regex_match = !strncmp(path_, handlers_[i].path, len - 1);
        }
        if ((exact_match || regex_match)
                && (handlers_[i].type == ANY || handlers_[i].type == request_type_)) {
            found = true;
            should_close = (handlers_[i].handler)(*this);
            break;
        }
    }

    if (!found) {
        send_error_code(404);
        // (*this) << F("URL not found: ");
        // client_->print(path_);
        // client_->println();
    }
    if (should_close) {
        client_.stop();
    }

    free(path_);
    free(request_type_str);
}
Beispiel #2
0
void TinyWebServer::process() {
  static uint8_t state = 0;
  static uint8_t handleridx = 0;
  switch (state) {
    case 0: {
      client_ = server_.available();
      if (!client_.connected() || !client_.available()) {
        return;
      }
      boolean is_complete = get_line(buffer, sizeof(buffer));
      if (!buffer[0]) {
        return;
      }
      if (!is_complete) {
        // The requested path is too long.
        send_error_code(414);
        client_.stop();
        return;
      }
      state=1;}
      break;
    case 1: {
      char* request_type_str = get_field(buffer, 0);
      request_type_ = UNKNOWN_REQUEST;
      if (!strcmp("GET", request_type_str)) {
        request_type_ = GET;
      } else if (!strcmp("POST", request_type_str)) {
        request_type_ = POST;
      } else if (!strcmp("PUT", request_type_str)) {
        request_type_ = PUT;
      }
      path_ = get_field(buffer, 1);
      free(request_type_str);
      state = 2;}
      break;
    case 2:
      // Process the headers.
      if (!process_headers()) {
        // Malformed header line.
        send_error_code(417);
        client_.stop();
        state=5; //??
        return;
      }
      state=3;
      break;
    case 3:
      // Header processing finished. Identify the handler to call.
      for (int i = 0; handlers_[i].path; i++) {
        int len = strlen_P(handlers_[i].path);
        boolean exact_match = !strcmp_P(path_, handlers_[i].path);
        boolean regex_match = false;
        if (pgm_read_byte(handlers_[i].path+len-1)=='*') {
          regex_match = !strncmp_P(path_, handlers_[i].path, len - 1);
        }
        if ((exact_match || regex_match)
          && (handlers_[i].type == ANY || handlers_[i].type == request_type_)) {
          handleridx=i;
          state=4;
          return;
        }
      }
        send_error_code(404);
        state=5;
      break;
    case 4:
      (handlers_[handleridx].handler)(*this); //this takes the longest
      state=5;
      break;
    case 5:
      client_.stop(); //we always close
      free(path_);
      state=0;
      break;
  }
}