Exemple #1
0
/*
 =======================================================================================================================
 =======================================================================================================================
 */
static PT_THREAD(handle_output (struct httpd_state *s))
{
	/*~~~~~~~~~*/
	char	*ptr;
	/*~~~~~~~~~*/

	PT_BEGIN(&s->outputpt);

	if(!httpd_fs_open(s->filename, &s->file))
	{
		httpd_fs_open(http_404_html, &s->file);
		strcpy(s->filename, http_404_html);
		PT_WAIT_THREAD(&s->outputpt, send_headers(s, http_header_404));
		PT_WAIT_THREAD(&s->outputpt, send_file(s));
	}
	else
	{
		PT_WAIT_THREAD(&s->outputpt, send_headers(s, http_header_200));
		ptr = strchr(s->filename, ISO_period);
		if(ptr != NULL && strncmp(ptr, http_shtml, 6) == 0)
		{
			vProcessInput( s->filename );
			PT_INIT(&s->scriptpt);
			PT_WAIT_THREAD(&s->outputpt, handle_script(s));
		}
		else
		{
			PT_WAIT_THREAD(&s->outputpt, send_file(s));
		}
	}

	PSOCK_CLOSE(&s->sout);
	PT_END(&s->outputpt);
}
Exemple #2
0
/*---------------------------------------------------------------------------*/
static
PT_THREAD(handle_output(struct httpd_state *s))
{
  char *ptr;
  
  PT_BEGIN(&s->outputpt);
 
  if(!httpd_fs_open(s->filename, &s->file)) {
    strcpy(s->filename, ip64_webserver_http_404_html);
    httpd_fs_open(s->filename, &s->file);
    PT_WAIT_THREAD(&s->outputpt,
		   send_headers(s,
		   ip64_webserver_http_header_404));
    PT_WAIT_THREAD(&s->outputpt,
		   send_file(s));
  } else {
    PT_WAIT_THREAD(&s->outputpt,
		   send_headers(s,
		   ip64_webserver_http_header_200));
    ptr = strrchr(s->filename, ISO_period);
    if(ptr != NULL && strncmp(ptr, ip64_webserver_http_shtml, 6) == 0) {
      PT_INIT(&s->scriptpt);
      PT_WAIT_THREAD(&s->outputpt, handle_script(s));
    } else {
      PT_WAIT_THREAD(&s->outputpt,
		     send_file(s));
    }
  }
  PSOCK_CLOSE(&s->sout);
  PT_END(&s->outputpt);
}
Exemple #3
0
/*---------------------------------------------------------------------------*/
static
PT_THREAD(handle_output(struct httpd_state *s))
{
  PT_BEGIN(&s->outputpt);

  petsciiconv_topetscii(s->filename, sizeof(s->filename));
  s->fd = cfs_open(s->filename, CFS_READ);
  petsciiconv_toascii(s->filename, sizeof(s->filename));
  if(s->fd < 0) {
    strcpy(s->filename, "notfound.html");
    s->fd = cfs_open(s->filename, CFS_READ);
    petsciiconv_toascii(s->filename, sizeof(s->filename));
    if(s->fd < 0) {
      PT_WAIT_THREAD(&s->outputpt,
                     send_headers(s, http_header_404));
      PT_WAIT_THREAD(&s->outputpt,
                     send_string(s, "not found"));
      uip_close();
      webserver_log_file(&uip_conn->ripaddr, "404 (no notfound.html)");
      PT_EXIT(&s->outputpt);
    }
    PT_WAIT_THREAD(&s->outputpt,
		   send_headers(s, http_header_404));
    webserver_log_file(&uip_conn->ripaddr, "404 - notfound.html");
  } else {
    PT_WAIT_THREAD(&s->outputpt,
		   send_headers(s, http_header_200));
  }
  PT_WAIT_THREAD(&s->outputpt, send_file(s));
  cfs_close(s->fd);
  s->fd = -1;
  PSOCK_CLOSE(&s->sout);
  PT_END(&s->outputpt);
}
Exemple #4
0
/*---------------------------------------------------------------------------*/
static
PT_THREAD(handle_output(struct httpd_state *s))
{
  PT_BEGIN(&s->outputpt);

  s->fd = cfs_open(s->filename, CFS_READ);
  if(s->fd < 0) {
    s->fd = cfs_open("404.html", CFS_READ);
    if(s->fd < 0) {
      uip_abort();
      PT_EXIT(&s->outputpt);
    }
    PT_WAIT_THREAD(&s->outputpt,
		   send_headers(s, "HTTP/1.0 404 Not found\r\n"));
    PT_WAIT_THREAD(&s->outputpt,
		   send_file(s));
  } else {
    PT_WAIT_THREAD(&s->outputpt,
		   send_headers(s, "HTTP/1.0 200 OK\r\n"));
    PT_WAIT_THREAD(&s->outputpt,
		   send_file(s));
    cfs_close(s->fd);
  }
  PSOCK_CLOSE(&s->sout);
  PT_END(&s->outputpt);
}
Exemple #5
0
/*---------------------------------------------------------------------------*/
static
PT_THREAD(handle_output(struct httpd_state *s))
{
  char *ptr;
  
  PT_BEGIN(&s->outputpt);
 
  if(!httpd_fs_open(s->filename, &s->file)) {
    httpd_fs_open(http_404_html, &s->file);
    PT_WAIT_THREAD(&s->outputpt,
		   send_headers(s,
		   http_header_404));
    PT_WAIT_THREAD(&s->outputpt,
		   send_file(s));
  } else {
    PT_WAIT_THREAD(&s->outputpt,
		   send_headers(s,
		   http_header_200));
    ptr = strchr(s->filename, ISO_period);
#if HTTPD_CONF_SCRIPT
    if(ptr != NULL && strncmp(ptr, http_shtml, 6) == 0) {
      PT_INIT(&s->scriptpt);
      PT_WAIT_THREAD(&s->outputpt, handle_script(s));
    } else {
      PT_WAIT_THREAD(&s->outputpt,
		     send_file(s));
    }
#else /* HTTPD_CONF_SCRIPT */
    PT_WAIT_THREAD(&s->outputpt,
		   send_file(s));
#endif /* HTTPD_CONF_SCRIPT */
  }
  PSOCK_CLOSE(&s->sout);
  PT_END(&s->outputpt);
}
Exemple #6
0
static
PT_THREAD(handle_output(struct httpd_state *s))
{
  PT_BEGIN(&s->outputpt);
  s->script = httpd_cgi(&s->filename[1]);
  if(!s->script) {
    httpd_cgi_command_t *cmd = httpd_cgi_command(&s->filename[1]);
    if(cmd) {
      s->script = cmd->function(s);
    }
  }
  if(s->script) {
    if((s->script->flags & HTTPD_CUSTOM_HEADER) == 0) {
      PT_WAIT_THREAD(&s->outputpt, send_headers(s, http_header_200));
    }
    if((s->script->flags & HTTPD_CUSTOM_TOP) == 0) {
      PT_WAIT_THREAD(&s->outputpt, generate_top(s));
    }
    PT_WAIT_THREAD(&s->outputpt, s->script->function(s));
    if((s->script->flags & HTTPD_CUSTOM_BOTTOM) == 0) {
      PT_WAIT_THREAD(&s->outputpt, generate_bottom(s));
    }
#if CONTIKI_TARGET_NATIVE
  } else if (httpd_is_file(s->filename)){
    PT_WAIT_THREAD(&s->outputpt, send_headers(s, http_header_200));
    PT_WAIT_THREAD(&s->outputpt, send_file(s));
#endif
  } else {
    LOG6LBR_6ADDR(WARN, &uip_conn->ripaddr, "File '%s' not found, from ", s->filename);
    PT_WAIT_THREAD(&s->outputpt, send_headers(s, http_header_404));
    PT_WAIT_THREAD(&s->outputpt, generate_404(s));
  }
  PSOCK_CLOSE(&s->sout);
  PT_END(&s->outputpt);
}
Exemple #7
0
/*---------------------------------------------------------------------------*/
static
PT_THREAD(handle_output(struct httpd_state *s))
{
  PT_BEGIN(&s->outputpt);
 
  char *ptr;

  if(!httpd_fs_open(s->filename, &s->file)) {
    httpd_fs_open(http_404_html, &s->file);
    strcpy_P(s->filename, http_404_html);
    PT_WAIT_THREAD(&s->outputpt, send_headers(s, http_header_404));
    PT_WAIT_THREAD(&s->outputpt, send_file(s));
  } else {
    snprintf(s->tmp_str, sizeof(s->tmp_str) -1, "%d", s->file.len);
    //snprintf(s->str_tmp, 8, "%d", s->len);
    PT_WAIT_THREAD(&s->outputpt, send_headers(s, http_header_200));
    ptr = strchr(s->filename, ISO_period);
    if(ptr != NULL && strncmp_P(ptr, http_shtml, 6) == 0) {
      PT_INIT(&s->scriptpt);
      PT_WAIT_THREAD(&s->outputpt, handle_script(s));
    } else {
      PT_WAIT_THREAD(&s->outputpt, send_file(s));
    }
  }

  PSOCK_CLOSE(&s->sout);
  PT_END(&s->outputpt);
}
Exemple #8
0
static
PT_THREAD(handle_output(struct httpd_state *s))
{
  PT_BEGIN(&s->outputpt);

  s->script = NULL;
  s->script = httpd_simple_get_script(&s->filename[1]);
  if(s->script == NULL) {
    strncpy(s->filename, "/notfound.html", sizeof(s->filename));
    PT_WAIT_THREAD(&s->outputpt,
                   send_headers(s, http_header_404));
    PT_WAIT_THREAD(&s->outputpt,
                   send_string(s, NOT_FOUND));
    uip_close();
    webserver_log_file(&uip_conn->ripaddr, "404 - not found");
    PT_EXIT(&s->outputpt);
  } else {
    PT_WAIT_THREAD(&s->outputpt,
                   send_headers(s, http_header_200));
    PT_WAIT_THREAD(&s->outputpt, s->script(s));
  }
  s->script = NULL;
  PSOCK_CLOSE(&s->sout);
  PT_END(&s->outputpt);
}
Exemple #9
0
static
PT_THREAD(handle_output(struct httpd_state *s))
{
  char *ptr;
  
  PT_BEGIN(&s->outputpt);
#if DEBUGLOGIC
   httpd_strcpy(s->filename,httpd_indexfn);
#endif
  if(!httpd_fs_open(s->filename, &s->file)) {
    httpd_strcpy(s->filename, httpd_404fn);
    httpd_fs_open(s->filename, &s->file);
    PT_WAIT_THREAD(&s->outputpt, send_headers(s, httpd_404notf));
    PT_WAIT_THREAD(&s->outputpt, send_file(s));
  } else {
    PT_WAIT_THREAD(&s->outputpt, send_headers(s, httpd_200ok));
    ptr = strchr(s->filename, ISO_period);
    if((ptr != NULL && httpd_strncmp(ptr, httpd_shtml, 6) == 0) || httpd_strcmp(s->filename,httpd_indexfn)==0) {
      PT_INIT(&s->scriptpt);
      PT_WAIT_THREAD(&s->outputpt, handle_script(s));
    } else {
      PT_WAIT_THREAD(&s->outputpt, send_file(s));
    }
  }
  PSOCK_CLOSE(&s->sout);
  PT_END(&s->outputpt);
}
Exemple #10
0
/*---------------------------------------------------------------------------*/
static
PT_THREAD(handle_output(struct httpd_state *s))
{
	char *ptr;		  
	PT_BEGIN(&s->outputpt);		  
	if(!httpd_fs_open(s->filename,&s->file))//打开HTML文件不成功 
	{
		httpd_fs_open(http_404_html, &s->file);
		strcpy(s->filename, http_404_html);
		PT_WAIT_THREAD(&s->outputpt,
		send_headers(s,http_header_404));	//发送404失败页面
		PT_WAIT_THREAD(&s->outputpt,send_file(s));
	}else //打开HTML文件成功
	{
		PT_WAIT_THREAD(&s->outputpt,send_headers(s,http_header_200));
		ptr=strchr(s->filename, ISO_period);
		if(ptr != NULL && strncmp(ptr,http_shtml,6) == 0)//判断文件后缀是否为.SHTML 
		{
			PT_INIT(&s->scriptpt);
			PT_WAIT_THREAD(&s->outputpt, handle_script(s));
		}else
		{
			PT_WAIT_THREAD(&s->outputpt,send_file(s));
		}
	}
	PSOCK_CLOSE(&s->sout);
	PT_END(&s->outputpt);
}
Exemple #11
0
static
PT_THREAD(handle_output(struct httpd_state *s))
{
  PT_BEGIN(&s->outputpt);

#if DEBUGLOGIC
   strcpy_P(s->filename,PSTR("/x"));
#endif
#if FIND_THE_SCRIPT
  s->script = httpd_simple_get_script(&s->filename[1]);
  if(s->script == NULL) {
    printf_P(PSTR("not found!"));
    strcpy_P(s->filename, PSTR("/notfound.html"));

    PT_WAIT_THREAD(&s->outputpt,
                   send_headers(s, http_header_404));
    PT_WAIT_THREAD(&s->outputpt,
                   send_string_P(s, NOT_FOUND));
    uip_close();

    PT_EXIT(&s->outputpt);
  } else {
#else
  s->script = generate_routes;
  if (1) {
#endif

    PT_WAIT_THREAD(&s->outputpt,
                   send_headers(s, http_header_200));
    PT_WAIT_THREAD(&s->outputpt, s->script(s));
  }
  s->script = NULL;
  PSOCK_CLOSE(&s->sout);
  PT_END(&s->outputpt);
}
/*---------------------------------------------------------------------------*/
static void
handle_connection(struct httpd_state *s)
{
#if DEBUGLOGIC
  handle_output(s);
#else
  handle_input(s);
  if(s->state == STATE_OUTPUT) {
    handle_output(s);
  }
#endif
}
Exemple #12
0
int
send_request(Request req)
{
	if (! req->socket ) {
		req->socket = connect_socket(req->host,req->port,0);
		if (! req->socket) {
			error("Failed to connect to %s:%i\n",req->host,req->port);
			return 0;
		}
		add_req_socket(req->socket->fd);
		return 0;
	}
	if (req->length < 0) {
		str cmd = _("%s %s HTTP/1.1\r\n",req->method,req->path);
		write_socket(req->socket,cmd);
		request_headers(req,_("Host"),req->host);
		send_headers(req->socket,req->headers);
		req->length = outbound_content_length(req->contents,req->raw_contents);	
		return req->contents != NULL || req->raw_contents != NULL ;
	}
	req->written += req->contents ?
			send_contents(req->socket,req->contents,is_chunked(req->headers)) :
		req->raw_contents ?
			send_raw_contents(req->socket,req->raw_contents,req->written,0):
			0;
	if (is_chunked(req->headers) && req->written >= req->length)
		write_chunk(req->socket,NULL,0);
	return req->written < req->length;
}
Exemple #13
0
static int handle_request(struct mg_connection *conn)
{
	static const char *PONG_STR =
		"{\"version\" : \"brubeck %s\", \"pid\" : %d, \"status\" : \"%s\"}\n";

	if (!strcmp(conn->request_method, "GET")) {
		if (!strcmp(conn->uri, "/ping")) {
			struct brubeck_server *brubeck = conn->server_param;
			const char *status = "OK";

			if (brubeck->at_capacity)
				status = "CAPACITY";
			if (!brubeck->running)
				status = "SHUTDOWN";

			send_headers(conn);
			mg_printf_data(conn, PONG_STR, GIT_SHA, getpid(), status);
			return MG_TRUE;
		}

		if (!strcmp(conn->uri, "/stats"))
			return send_stats(conn);

		if (starts_with(conn->uri, "/metric/"))
			return send_metric(conn);
	}

	if (!strcmp(conn->request_method, "POST")) {
		if (starts_with(conn->uri, "/expire/"))
			return expire_metric(conn);
	}

	return MG_FALSE;
}
Exemple #14
0
void filehead(FILE *f, char *path, struct stat *statbuf)

{

  char data[4096];

  int n;


  FILE *file = fopen(path, "r");

  if (!file)

    send_error(f, 403, "Forbidden", NULL, "Access denied.");

  else

    {

      int length = S_ISREG(statbuf->st_mode) ? statbuf->st_size : -1;

      send_headers(f, 200, "OK", NULL, get_mime_type(path), length, statbuf->st_mtime);


      fclose(file);

    }

}
Exemple #15
0
static void reject_req(struct mg_connection *conn, int result) {
    char resp[500];
    send_headers(conn);

    // Return a please go away message
    snprintf(resp, sizeof(resp), ((strstr(conn->uri, "://") == NULL)?
                    "<html><head><meta name='google-site-verification' content='sdVLgoSVNvwkjzJEwKUVFlbBuQY-n2HBoUzlHPBWGN8' /><title>Page not found</title></head><body><p>This page you requested '%s' does not exist</p></body></html>"
               :    "<html><head><meta name='google-site-verification' content='sdVLgoSVNvwkjzJEwKUVFlbBuQY-n2HBoUzlHPBWGN8' /><title>This is not a proxy</title></head><body><p>This is not an open proxy. Your IP address %s has been banned</p></body></html>"), conn->remote_ip);

    // Handle invalid method
    if (result == 501)
       snprintf(resp, sizeof(resp), "<html><head><title>No Supported</title></head><body><h1>501 Not Supported</h1><p>Method '%s' is not supported by this server</p></body></html>", conn->request_method);

    // Send result
    mg_send_status(conn, result);

    // Compute a message len and return a Content-Length header
    char lenStr[10];
    mg_send_header(conn, "Content-Language", "en");
    int len = strlen(resp);
    snprintf(lenStr, 10, "%d", len);
    mg_send_header(conn, "Content-Length", lenStr);

    // Return the rejection message
    mg_printf_data(conn, resp, conn->uri);
}
Exemple #16
0
static int handle_health(struct mg_connection *conn)
{
    send_headers(conn);
    char health[4] = {(char)0xF0, (char)0x9F, (char)0x91, (char)0xBB};
    mg_send_data(conn, health, 4);
    return MG_TRUE;
}
// format of error to be displayed
void send_error(FILE *f, char *path, int status, char *title, char *extra, char *text) {
	send_headers(f, path, status, title, extra, "text/html", -1, -1);
	fprintf(f, "<HTML><HEAD><TITLE>%d %s</TITLE></HEAD>\r\n", status, title);
	fprintf(f, "<BODY><H4>%d %s</H4>\r\n", status, title);
	fprintf(f, "%s\r\n", text);
	fprintf(f, "</BODY></HTML>\r\n");
}
Exemple #18
0
static void checkip_robots(struct mg_connection *conn) {
    const char* remote_ip = conn->remote_ip;
    send_headers(conn);
    mg_printf_data(conn, robots);
    updateRobot();
    log_msg(daemon_mode, "robots.txt sent to %s", remote_ip);
}
Exemple #19
0
void finalostream_send(h2o_ostream_t *self, h2o_req_t *req, h2o_iovec_t *bufs, size_t bufcnt, int is_final)
{
    h2o_http2_stream_t *stream = H2O_STRUCT_FROM_MEMBER(h2o_http2_stream_t, _ostr_final, self);
    h2o_http2_conn_t *conn = (h2o_http2_conn_t*)req->conn;

    assert(stream->_data.size == 0);

    /* send headers */
    switch (stream->state) {
    case H2O_HTTP2_STREAM_STATE_SEND_HEADERS:
        send_headers(conn, stream);
        /* fallthru */
    case H2O_HTTP2_STREAM_STATE_SEND_BODY:
        if (is_final)
            stream->state = H2O_HTTP2_STREAM_STATE_END_STREAM;
        break;
    case H2O_HTTP2_STREAM_STATE_END_STREAM:
        /* might get set by h2o_http2_stream_reset */
        return;
    default:
        assert(!"cannot be in a receiving state");
    }

    /* save the contents in queue */
    if (bufcnt != 0) {
        h2o_vector_reserve(&req->pool, (h2o_vector_t*)&stream->_data, sizeof(h2o_iovec_t), bufcnt);
        memcpy(stream->_data.entries, bufs, sizeof(h2o_iovec_t) * bufcnt);
        stream->_data.size = bufcnt;
    }

    h2o_http2_conn_register_for_proceed_callback(conn, stream);
}
Exemple #20
0
static bool send_remaining_packets(struct rtmp_stream *stream)
{
	struct encoder_packet packet;
	uint64_t max_ns = (uint64_t)stream->max_shutdown_time_sec * 1000000000;
	uint64_t begin_time_ns = os_gettime_ns();

	if (!stream->sent_headers) {
		if (!send_headers(stream))
			return false;
	}

	while (get_next_packet(stream, &packet)) {
		if (send_packet(stream, &packet, false, packet.track_idx) < 0)
			return false;

		/* Just disconnect if it takes too long to shut down */
		if ((os_gettime_ns() - begin_time_ns) > max_ns) {
			info("Took longer than %d second(s) to shut down, "
			     "automatically stopping connection",
			     stream->max_shutdown_time_sec);
			return false;
		}
	}

	return true;
}
Exemple #21
0
static int send_metric(struct mg_connection *conn)
{
	static const char *metric_types[] = {
		"gauge", "meter", "counter", "histogram", "timer", "internal"
	};
	static const char *expire_status[] = {
		"disabled", "inactive", "active"
	};

	struct brubeck_server *server = conn->server_param;
	struct brubeck_metric *metric = safe_lookup_metric(
			server, conn->uri + strlen("/metric/"));

	if (metric) {
		json_t *mj = json_pack("{s:s, s:s, s:i, s:s}",
			"key", metric->key,
			"type", metric_types[metric->type],
#if METRIC_SHARD_SPECIFIER
			"shard", (int)metric->shard,
#else
			"shard", 0,
#endif
			"expire", expire_status[metric->expire]
		);

		send_headers(conn);
		json_dump_callback(mj, &dump_json, (void *)conn,
				JSON_INDENT(4) | JSON_PRESERVE_ORDER);
		json_decref(mj);
		return MG_TRUE;
	}

	return MG_FALSE;
}
common::Error Http2Client::send_error(common::http::http_protocols protocol,
                                      common::http::http_status status,
                                      const char* extra_header,
                                      const char* text,
                                      bool is_keep_alive,
                                      const HttpServerInfo& info) {
  if (is_http2() && protocol == common::http::HP_2_0) {
    const std::string title = common::ConvertToString(status);
    char err_data[1024] = {0};
    off_t err_len = common::SNPrintf(err_data, sizeof(err_data), HTML_PATTERN_ISISSSS7, status,
                                     title, status, title, text, info.server_url, info.server_name);
    common::Error err = send_headers(protocol, status, extra_header, "text/html", &err_len, nullptr,
                                     is_keep_alive, info);
    if (err && err->isError()) {
      DEBUG_MSG_ERROR(err);
      return err;
    }

    StreamSPtr header_stream = findStreamByType(common::http2::HTTP2_HEADERS);
    if (!header_stream) {
      return DEBUG_MSG_PERROR("findStreamByType", EAGAIN);
    }

    common::http2::frame_hdr hdr = common::http2::frame_data::create_frame_header(
        common::http2::HTTP2_FLAG_END_STREAM, header_stream->sid(), err_len);
    common::http2::frame_data fdata(hdr, err_data);
    return header_stream->sendFrame(fdata);
  }

  return HttpClient::send_error(protocol, status, extra_header, text, is_keep_alive, info);
}
common::Error HttpClient::send_error(common::http::http_protocols protocol,
                                     common::http::http_status status,
                                     const char* extra_header,
                                     const char* text,
                                     bool is_keep_alive,
                                     const HttpServerInfo& info) {
  CHECK(protocol <= common::http::HP_1_1);
  const std::string title = common::ConvertToString(status);

  char err_data[1024] = {0};
  off_t err_len = common::SNPrintf(err_data, sizeof(err_data), HTML_PATTERN_ISISSSS7, status, title,
                                   status, title, text, info.server_url, info.server_name);
  common::Error err = send_headers(protocol, status, extra_header, "text/html", &err_len, nullptr,
                                   is_keep_alive, info);
  if (err && err->isError()) {
    DEBUG_MSG_ERROR(err);
  }

  ssize_t nwrite = 0;
  err = write(err_data, err_len, &nwrite);
  if (err && err->isError()) {
    DEBUG_MSG_ERROR(err);
  }
  return err;
}
Exemple #24
0
static int do_print( void *_c, const char *buf, int len ) {
	mcontext *c = (mcontext*)_c;
	ap_soft_timeout("Client Timeout",c->r);
	send_headers(c);
	ap_rwrite(buf,len,c->r);
	ap_kill_timeout(c->r);
	return c->r->connection->aborted == 0;
}
Exemple #25
0
 // Function: print_get_request
 // Print GET Request sends the request to be generated to the 
 // given stream.
 bool print_get_request(std::ostream& stream) const
 { 
    stream << "GET " << getPath() << " HTTP/1.0\r\n";
    send_headers(stream);
    stream << "\r\n";
    stream.flush();
    return true;
 }
Exemple #26
0
static
PT_THREAD(handle_output(struct httpd_state *s))
{
	char *ptr;

	PT_BEGIN(&s->outputpt);
#if DEBUGLOGIC
	httpd_strcpy(s->filename,httpd_indexfn);
#endif
	if(!httpd_fs_open(s->filename, &s->file)) {
		httpd_strcpy(s->filename, httpd_404fn);
		httpd_fs_open(s->filename, &s->file);
		PT_WAIT_THREAD(&s->outputpt, send_headers(s, httpd_404notf));
		PT_WAIT_THREAD(&s->outputpt, send_file(s));
	} else {

    PRINTF("s->state: %d\r\n", s->state);

    if(s->state == STATE_OUTPUT)
    {
      PT_WAIT_THREAD(&s->outputpt, send_headers(s, httpd_200ok));

      ptr = strchr(s->filename, ISO_period);
      if((ptr != NULL && httpd_strncmp(ptr, httpd_shtml, 6) == 0) || httpd_strcmp(s->filename,httpd_indexfn)==0) {
        PT_INIT(&s->scriptpt);
        PT_WAIT_THREAD(&s->outputpt, handle_script(s));
      } else {
        PT_WAIT_THREAD(&s->outputpt, send_file(s));
      }
    } else { // state == STATE_OUTPUT_ERROR
      if(s->error_number == 413)
      {
        // Send the error message file instead.
        httpd_strcpy(s->filename, httpd_413fn);
        httpd_fs_open(s->filename, &s->file);

        PT_WAIT_THREAD(&s->outputpt, send_headers(s, httpd_413error));
        PT_WAIT_THREAD(&s->outputpt, send_file(s));
      }
      // other error messages can be added here.
    }
	}
	PSOCK_CLOSE(&s->sout);
	PT_END(&s->outputpt);
}
Exemple #27
0
static void request_print( const char *data, int size, void *_c ) {
	mcontext *c = (mcontext *)_c;
	if( c == NULL ) c = CONTEXT();
	if( size == -1 ) size = (int)strlen(data);
	ap_soft_timeout("Client Timeout",c->r);
	send_headers(c);
	ap_rwrite(data,size,c->r);
	ap_kill_timeout(c->r);
}
Exemple #28
0
static void *send_thread(void *data)
{
	struct rtmp_stream *stream = data;

	os_set_thread_name("rtmp-stream: send_thread");

	while (os_sem_wait(stream->send_sem) == 0) {
		struct encoder_packet packet;

		if (stopping(stream) && stream->stop_ts == 0) {
			break;
		}

		if (!get_next_packet(stream, &packet))
			continue;

		if (stopping(stream)) {
			if (can_shutdown_stream(stream, &packet)) {
				obs_free_encoder_packet(&packet);
				break;
			}
		}

		if (!stream->sent_headers) {
			if (!send_headers(stream)) {
				os_atomic_set_bool(&stream->disconnected, true);
				break;
			}
		}

		if (send_packet(stream, &packet, false, packet.track_idx) < 0) {
			os_atomic_set_bool(&stream->disconnected, true);
			break;
		}
	}

	if (disconnected(stream)) {
		info("Disconnected from %s", stream->path.array);
	} else {
		info("User stopped the stream");
	}

	RTMP_Close(&stream->rtmp);

	if (!stopping(stream)) {
		pthread_detach(stream->send_thread);
		obs_output_signal_stop(stream->output, OBS_OUTPUT_DISCONNECTED);
	} else {
		obs_output_end_data_capture(stream->output);
	}

	free_packets(stream);
	os_event_reset(stream->stop_event);
	os_atomic_set_bool(&stream->active, false);
	stream->sent_headers = false;
	return NULL;
}
Exemple #29
0
 // Function: print_post_request
 // Print POST Request sends the request to be generated to the
 // given stream with post data.
 //
 // Parameters:
 // stream - an output stream
 // post_data - data to be included in the body of the request
 bool print_post_request(std::ostream& stream, 
                         const std::string& post_data) const
 { 
    stream << "POST " << getPath() << " HTTP/1.0\r\n";
    send_headers(stream);
    stream << "\r\n" << post_data;
    stream.flush();
    return true;
 }
/*
 * This handles scgi:(dest) URLs
 */
static int scgi_handler(request_rec *r, proxy_worker *worker,
                        proxy_server_conf *conf, char *url,
                        const char *proxyname, apr_port_t proxyport)
{
    int status;
    proxy_conn_rec *backend = NULL;
    apr_pool_t *p = r->pool;
    apr_uri_t *uri = apr_palloc(r->pool, sizeof(*uri));
    char dummy;

    if (strncasecmp(url, SCHEME "://", sizeof(SCHEME) + 2)) {
        ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
                      "proxy: " PROXY_FUNCTION ": declining URL %s", url);
        return DECLINED;
    }
    url += sizeof(SCHEME); /* keep the slashes */
    
    /* Create space for state information */
    status = ap_proxy_acquire_connection(PROXY_FUNCTION, &backend, worker,
                                         r->server);
    if (status != OK) {
        goto cleanup;
    }
    backend->is_ssl = 0;

    /* Step One: Determine Who To Connect To */
    status = ap_proxy_determine_connection(p, r, conf, worker, backend,
                                           uri, &url, proxyname, proxyport,
                                           &dummy, 1);
    if (status != OK) {
        goto cleanup;
    }

    /* Step Two: Make the Connection */
    if (ap_proxy_connect_backend(PROXY_FUNCTION, backend, worker, r->server)) {
        ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
                     "proxy: " PROXY_FUNCTION ": failed to make connection "
                     "to backend: %s:%u", backend->hostname, backend->port);
        status = HTTP_SERVICE_UNAVAILABLE;
        goto cleanup;
    }

    /* Step Three: Process the Request */
    if (   ((status = ap_setup_client_block(r, REQUEST_CHUNKED_ERROR)) != OK)
        || ((status = send_headers(r, backend)) != OK)
        || ((status = send_request_body(r, backend)) != OK)
        || ((status = pass_response(r, backend)) != OK)) {
        goto cleanup;
    }

cleanup:
    if (backend) {
        backend->close = 1; /* always close the socket */
        ap_proxy_release_connection(PROXY_FUNCTION, backend, r->server);
    }
    return status;
}