예제 #1
0
파일: mp3d.cpp 프로젝트: JoeBargo/enh
static int handle_request(void* /*cls*/, MHD_Connection* connection, const char* url, const char* method, const char*, const char*, unsigned int*, void** /*con_cls*/) {
  if (std::string(method) != "GET") {
    return report_error(connection, MHD_HTTP_NOT_IMPLEMENTED, "501 Not Implemented");
  }
  
  if (mp3d_debug_httpd) {
    MHD_get_connection_values(connection, MHD_HEADER_KIND, dump_key, NULL);
    MHD_get_connection_values(connection, MHD_COOKIE_KIND, dump_key, NULL);
    MHD_get_connection_values(connection, MHD_GET_ARGUMENT_KIND, dump_key, NULL);
    std::cout << "*** " << method << " request for '" << url << "'" << std::endl;
  }
  
  const char* q = MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "mp3d_q");
  
  const std::string request_url(url);
  if (request_url == "/") {
    std::string page_data;
    make_main_page(page_data, q);
    return send_string_response(connection, MHD_HTTP_OK, page_data);
  } else if (boost::starts_with(request_url, "/static/")) {
    StringStringMap::const_iterator it = static_file_map.find(request_url);
    if (it == static_file_map.end()) {
      return report_error(connection, MHD_HTTP_NOT_FOUND, "404 Not Found");
    }
    return send_string_response(connection, MHD_HTTP_OK, it->second);
  } else if (boost::starts_with(request_url, "/play/")) {
    const size_t id = strtoul(request_url.substr(6).c_str(), 0, 10); // FIXME: error checking.
    play_queue.clear();
    play_queue.push_back(all_mp3s[id]); // FIXME: lookup by id (don't assume id == index).
    return see_other(connection, "/");
  } else if (boost::starts_with(request_url, "/add/")) {
    const size_t id = strtoul(request_url.substr(5).c_str(), 0, 10); // FIXME: error checking.
    play_queue.push_back(all_mp3s[id]); // FIXME: lookup by id (don't assume id == index).
    return see_other(connection, "/");
  } else if (boost::starts_with(request_url, "/remove/")) {
    const size_t id = strtoul(request_url.substr(8).c_str(), 0, 10); // FIXME: error checking.
    play_queue.remove(id); // FIXME: is this set-like behavior the behavior we want?
    return see_other(connection, "/");
  } else {
    return report_error(connection, MHD_HTTP_NOT_FOUND, "404 Not Found");
  }
}
예제 #2
0
void run_js(void *jsargs) {
  js_call *call_data = (js_call *) jsargs;
  spidermonkey_drv_t *dd = call_data->driver_data;
  ErlDrvBinary *args = call_data->args;
  driver_free(call_data);
  char *data = args->orig_bytes;
  char *command = read_command(&data);
  char *call_id = read_string(&data);
  char *result = NULL;
  if (strncmp(command, "ej", 2) == 0) {
    char *filename = read_string(&data);
    char *code = read_string(&data);
    result = sm_eval(dd->vm, filename, code, 1);
    if (strstr(result, "{\"error\"") != NULL) {
      send_error_string_response(dd, call_id, result);
    }
    else {
      send_string_response(dd, call_id, result);
    }
    driver_free(filename);
    driver_free(code);
    driver_free(result);
  }
  else if (strncmp(command, "dj", 2) == 0) {
    char *filename = read_string(&data);
    char *code = read_string(&data);
    result = sm_eval(dd->vm, filename, code, 0);
    if (result == NULL) {
      send_ok_response(dd, call_id);
    }
    else {
      send_error_string_response(dd, call_id, result);
      driver_free(result);
    }
    driver_free(filename);
    driver_free(code);
  }
  else if (strncmp(command, "sd", 2) == 0) {
    dd->shutdown = 1;
    send_ok_response(dd, call_id);
  }
  else {
    unknown_command(dd, call_id);
  }
  driver_free(command);
  driver_free(call_id);
  driver_binary_dec_refc(args);
}
예제 #3
0
파일: mp3d.cpp 프로젝트: JoeBargo/enh
static int report_error(MHD_Connection* connection, int code, const std::string& text) {
  const std::string error = "<html><body><h1>" + text + "</h1></body></html>";
  return send_string_response(connection, code, error);
}