示例#1
0
LWS_VISIBLE int lws_serve_http_file(struct lws_context *context,
				    struct lws *wsi, const char *file,
				    const char *content_type,
				    const char *other_headers,
				    int other_headers_len)
{
	unsigned char *response = context->service_buffer +
				  LWS_SEND_BUFFER_PRE_PADDING;
	unsigned char *p = response;
	unsigned char *end = p + sizeof(context->service_buffer) -
			     LWS_SEND_BUFFER_PRE_PADDING;
	int ret = 0;

	wsi->u.http.fd = lws_plat_open_file(file, &wsi->u.http.filelen);

	if (wsi->u.http.fd == LWS_INVALID_FILE) {
		lwsl_err("Unable to open '%s'\n", file);
		lws_return_http_status(context, wsi, HTTP_STATUS_NOT_FOUND,
				       NULL);
		return -1;
	}

	if (lws_add_http_header_status(context, wsi, 200, &p, end))
		return -1;
	if (lws_add_http_header_by_token(context, wsi, WSI_TOKEN_HTTP_SERVER,
					 (unsigned char *)"libwebsockets", 13,
					 &p, end))
		return -1;
	if (lws_add_http_header_by_token(context, wsi,
					 WSI_TOKEN_HTTP_CONTENT_TYPE,
					 (unsigned char *)content_type,
					 strlen(content_type), &p, end))
		return -1;
	if (lws_add_http_header_content_length(context, wsi,
					       wsi->u.http.filelen, &p, end))
		return -1;

	if (other_headers) {
		if ((end - p) < other_headers_len)
			return -1;
		memcpy(p, other_headers, other_headers_len);
		p += other_headers_len;
	}

	if (lws_finalize_http_header(context, wsi, &p, end))
		return -1;
	
	ret = lws_write(wsi, response,
				   p - response, LWS_WRITE_HTTP_HEADERS);
	if (ret != (p - response)) {
		lwsl_err("_write returned %d from %d\n", ret, (p - response));
		return -1;
	}

	wsi->u.http.filepos = 0;
	wsi->state = WSI_STATE_HTTP_ISSUING_FILE;

	return lws_serve_http_file_fragment(context, wsi);
}
示例#2
0
LWS_VISIBLE int libwebsockets_serve_http_file(
    struct libwebsocket_context *context,
    struct libwebsocket *wsi, const char *file,
    const char *content_type, const char *other_headers)
{
    unsigned char *p = context->service_buffer;
    int ret = 0;
    int n;

    wsi->u.http.fd = lws_plat_open_file(file, &wsi->u.http.filelen);

    if (wsi->u.http.fd == LWS_INVALID_FILE) {
        lwsl_err("Unable to open '%s'\n", file);
        libwebsockets_return_http_status(context, wsi,
                                         HTTP_STATUS_NOT_FOUND, NULL);
        return -1;
    }

    p += sprintf((char *)p,
                 "HTTP/1.0 200 OK\x0d\x0aServer: libwebsockets\x0d\x0a""Content-Type: %s\x0d\x0a",
                 content_type);
    if (other_headers) {
        n = strlen(other_headers);
        memcpy(p, other_headers, n);
        p += n;
    }
    p += sprintf((char *)p,
                 "Content-Length: %lu\x0d\x0a\x0d\x0a", wsi->u.http.filelen);

    ret = libwebsocket_write(wsi, context->service_buffer,
                             p - context->service_buffer, LWS_WRITE_HTTP);
    if (ret != (p - context->service_buffer)) {
        lwsl_err("_write returned %d from %d\n", ret, (p - context->service_buffer));
        return -1;
    }

    wsi->u.http.filepos = 0;
    wsi->state = WSI_STATE_HTTP_ISSUING_FILE;

    return libwebsockets_serve_http_file_fragment(context, wsi);
}