diff options
author | Christian Grothoff <christian@grothoff.org> | 2020-07-26 14:15:50 +0200 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2020-07-26 14:15:50 +0200 |
commit | 1c4f23db7458d79a04c8e68022ade67cb5ee006b (patch) | |
tree | ca30e21e645f50d9a230ae29ef3cf21056a96279 /src/microhttpd/response.c | |
parent | babda9e9c702aee6e03e7537108383e32a7c17d0 (diff) | |
download | libmicrohttpd-1c4f23db7458d79a04c8e68022ade67cb5ee006b.tar.gz libmicrohttpd-1c4f23db7458d79a04c8e68022ade67cb5ee006b.zip |
add ability to serve files from pipe
Diffstat (limited to 'src/microhttpd/response.c')
-rw-r--r-- | src/microhttpd/response.c | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/microhttpd/response.c b/src/microhttpd/response.c index 25c05163..27b3ee3f 100644 --- a/src/microhttpd/response.c +++ b/src/microhttpd/response.c | |||
@@ -527,6 +527,37 @@ file_reader (void *cls, | |||
527 | 527 | ||
528 | 528 | ||
529 | /** | 529 | /** |
530 | * Given a pipe descriptor, read data from the pipe | ||
531 | * to generate the response. | ||
532 | * | ||
533 | * @param cls pointer to the response | ||
534 | * @param pos offset in the pipe to access (ignored) | ||
535 | * @param buf where to write the data | ||
536 | * @param max number of bytes to write at most | ||
537 | * @return number of bytes written | ||
538 | */ | ||
539 | static ssize_t | ||
540 | pipe_reader (void *cls, | ||
541 | uint64_t pos, | ||
542 | char *buf, | ||
543 | size_t max) | ||
544 | { | ||
545 | struct MHD_Response *response = cls; | ||
546 | ssize_t n; | ||
547 | |||
548 | (void) pos; | ||
549 | n = read (response->fd, | ||
550 | buf, | ||
551 | max); | ||
552 | if (0 == n) | ||
553 | return MHD_CONTENT_READER_END_OF_STREAM; | ||
554 | if (n < 0) | ||
555 | return MHD_CONTENT_READER_END_WITH_ERROR; | ||
556 | return n; | ||
557 | } | ||
558 | |||
559 | |||
560 | /** | ||
530 | * Destroy file reader context. Closes the file | 561 | * Destroy file reader context. Closes the file |
531 | * descriptor. | 562 | * descriptor. |
532 | * | 563 | * |
@@ -614,6 +645,7 @@ MHD_create_response_from_fd_at_offset64 (uint64_t size, | |||
614 | if (NULL == response) | 645 | if (NULL == response) |
615 | return NULL; | 646 | return NULL; |
616 | response->fd = fd; | 647 | response->fd = fd; |
648 | response->is_pipe = false; | ||
617 | response->fd_off = offset; | 649 | response->fd_off = offset; |
618 | response->crc_cls = response; | 650 | response->crc_cls = response; |
619 | return response; | 651 | return response; |
@@ -622,6 +654,35 @@ MHD_create_response_from_fd_at_offset64 (uint64_t size, | |||
622 | 654 | ||
623 | /** | 655 | /** |
624 | * Create a response object. The response object can be extended with | 656 | * Create a response object. The response object can be extended with |
657 | * header information and then be used ONLY ONCE. | ||
658 | * | ||
659 | * @param fd file descriptor referring to a read-end of a pipe with the | ||
660 | * data; will be closed when response is destroyed; | ||
661 | * fd should be in 'blocking' mode | ||
662 | * @return NULL on error (i.e. invalid arguments, out of memory) | ||
663 | * @ingroup response | ||
664 | */ | ||
665 | _MHD_EXTERN struct MHD_Response * | ||
666 | MHD_create_response_from_pipe (int fd) | ||
667 | { | ||
668 | struct MHD_Response *response; | ||
669 | |||
670 | response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN, | ||
671 | MHD_FILE_READ_BLOCK_SIZE, | ||
672 | &pipe_reader, | ||
673 | NULL, | ||
674 | &free_callback); | ||
675 | if (NULL == response) | ||
676 | return NULL; | ||
677 | response->fd = fd; | ||
678 | response->is_pipe = true; | ||
679 | response->crc_cls = response; | ||
680 | return response; | ||
681 | } | ||
682 | |||
683 | |||
684 | /** | ||
685 | * Create a response object. The response object can be extended with | ||
625 | * header information and then be used any number of times. | 686 | * header information and then be used any number of times. |
626 | * | 687 | * |
627 | * @param size size of the data portion of the response | 688 | * @param size size of the data portion of the response |