# Internals: how do I send large file to the client?

**URL:** <https://www.ruby-forum.com/t/internals-how-do-i-send-large-file-to-the-client/245982>\
**Category:** NGINX\
**Created:** [August 12, 2013, 9:31pm UTC](https://www.ruby-forum.com/t/internals-how-do-i-send-large-file-to-the-client/245982 "2013-08-12T21:31:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sadaf\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@Sadaf\_N](https://www.ruby-forum.com/u/Sadaf_N)\
**Post date:** [August 12, 2013, 9:31pm UTC](https://www.ruby-forum.com/t/internals-how-do-i-send-large-file-to-the-client/245982/1 "2013-08-12T21:31:40Z")

</div>

Hi,

I’m writing a filter module which implies a backend to be sending XML  
with  
information about files that have to be concatenated and sent to the  
client.

One way to send a file is to `ngx_read_file` into a buffer allocated in  
the  
heap(pool) and push it onto the chain. However, I obviously can’t  
allocate  
~10G  
in the heap. I have to send it chunk-by-chunk. How do I perform such  
kind of  
I/O?

Regards.

Posted at Nginx Forum:

> **[Internals: how do I send large file to the client?](https://forum.nginx.org/read.php?2,241796,241796#msg-241796)**
>
> Hi, I'm writing a filter module which implies a backend to be sending XML with information about files that have to be concatenated and sent to the client. One way to send a file is to \`ngx\_read\_file\` into a buffer allocated in the heap(pool) and...

---

<div class="post-metadata">

**Author:** ![ruslan\_osmanov](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/3e96dc/32.png) [@ruslan\_osmanov](https://www.ruby-forum.com/u/ruslan_osmanov)\
**Post date:** [August 14, 2013, 2:10pm UTC](https://www.ruby-forum.com/t/internals-how-do-i-send-large-file-to-the-client/245982/2 "2013-08-14T14:10:04Z")

</div>

have u know the split command in linux . you can use that to split file  
then send it after you can use join command to join files

---

<div class="post-metadata">

**Author:** ![ruslan\_osmanov](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/3e96dc/32.png) [@ruslan\_osmanov](https://www.ruby-forum.com/u/ruslan_osmanov)\
**Post date:** [August 14, 2013, 4:32pm UTC](https://www.ruby-forum.com/t/internals-how-do-i-send-large-file-to-the-client/245982/3 "2013-08-14T16:32:36Z")

</div>

I know those commands. But the question was about Nginx’s internals. I  
thought  
somebody would suggest a pseudo-code snippet similar to the following:

ngx\_buf\_t b;  
size\_t length = 0;

loop (files as file) {  
…

```
u_char *filename = file->name;

if (ngx_open_cached_file(ccf->open_file_cache, filename, &of, 

```

r-\>pool)  
!= NGX\_OK)  
return NGX\_ERROR;

```
length += of.size;

b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
if (b == NULL) {
    return NGX_HTTP_INTERNAL_SERVER_ERROR;
}

b->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t));
if (b->file == NULL) {
    return NGX_HTTP_INTERNAL_SERVER_ERROR;
}

b->file_pos = 0;
b->file_last = of.size;
b->in_file = b->file_last ? 1 : 0;
b->file->fd = of.fd;
b->file->name = *filename;
b->file->log = r->connection->log;
b->file->directio = of.is_directio;

cl = ngx_alloc_chain_link(r->pool);
if (cl == NULL) {
    return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
cl->buf = b;

*last_out = cl;
last_out = &cl->next;
cl->next = NULL;

...

```

}

I’ve found ngx\_open\_cached\_file and ngx\_alloc\_chain\_link just recently.  
I  
see,  
there should be a way to chain open files without actually performing  
I/O  
myself. Still have no clear understanding how it works and how one  
should  
use  
the cached files’ API.

removed\_email\_address@domain.invalid Wrote:
