Repository navigation
Expand file tree
/
Copy pathserver-http-file.html
More file actions
514 lines (361 loc) · 28.2 KB
/
Copy pathserver-http-file.html
File metadata and controls
514 lines (361 loc) · 28.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Serving static files — Armeria 0.82.0 documentation</title>
<link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="_static/overrides.css" type="text/css" />
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="Embedding a servlet container" href="server-servlet.html" />
<link rel="prev" title="Annotated services" href="server-annotated-service.html" />
<script src="_static/center_page.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.15.6/styles/default.min.css">
<script src="_static/js/modernizr.min.js"></script>
</head>
<body class="wy-body-for-nav">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search">
<a href="index.html">
<img src="_static/armeria.png" class="logo" alt="Logo"/>
</a>
<div class="version">
0.82
</div>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="search.html" method="get">
<input type="text" name="q" placeholder="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="setup.html">Setting up a project</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="server.html">Writing a server</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="server-basics.html">Server basics</a></li>
<li class="toctree-l2"><a class="reference internal" href="server-decorator.html">Decorating a service</a></li>
<li class="toctree-l2"><a class="reference internal" href="server-grpc.html">Running a gRPC service</a></li>
<li class="toctree-l2"><a class="reference internal" href="server-thrift.html">Running a Thrift service</a></li>
<li class="toctree-l2"><a class="reference internal" href="server-docservice.html">Browsing and invoking services with <tt class="docutils literal">DocService</tt></a></li>
<li class="toctree-l2"><a class="reference internal" href="server-annotated-service.html">Annotated services</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">Serving static files</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#auto-generating-directory-listings">Auto-generating directory listings</a></li>
<li class="toctree-l3"><a class="reference internal" href="#specifying-additional-response-headers">Specifying additional response headers</a></li>
<li class="toctree-l3"><a class="reference internal" href="#adjusting-static-file-cache">Adjusting static file cache</a></li>
<li class="toctree-l3"><a class="reference internal" href="#serving-pre-compressed-files">Serving pre-compressed files</a></li>
<li class="toctree-l3"><a class="reference internal" href="#serving-an-individual-file">Serving an individual file</a></li>
<li class="toctree-l3"><a class="reference internal" href="#configuring-httpfile">Configuring <tt class="docutils literal">HttpFile</tt></a></li>
<li class="toctree-l3"><a class="reference internal" href="#caching-httpfile">Caching <tt class="docutils literal">HttpFile</tt></a></li>
<li class="toctree-l3"><a class="reference internal" href="#aggregating-httpfile">Aggregating <tt class="docutils literal">HttpFile</tt></a></li>
<li class="toctree-l3"><a class="reference internal" href="#building-aggregatedhttpfile-from-httpdata">Building <tt class="docutils literal">AggregatedHttpFile</tt> from <tt class="docutils literal">HttpData</tt></a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="server-servlet.html">Embedding a servlet container</a></li>
<li class="toctree-l2"><a class="reference internal" href="server-access-log.html">Writing an access log</a></li>
<li class="toctree-l2"><a class="reference internal" href="server-cors.html">Configuring CORS</a></li>
<li class="toctree-l2"><a class="reference internal" href="server-sse.html">Serving Server-Sent Events</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="client.html">Writing a client</a></li>
<li class="toctree-l1"><a class="reference internal" href="advanced.html">Advanced topics</a></li>
<li class="toctree-l1"><a class="reference external" href="https://github.com/line/armeria/releases">Release notes</a></li>
<li class="toctree-l1"><a class="reference external" href="apidocs/index.html#://">API documentation</a></li>
<li class="toctree-l1"><a class="reference external" href="xref/index.html#://">Source cross-reference</a></li>
<li class="toctree-l1"><a class="reference external" href="https://github.com/line/armeria/issues?q=label%3Aquestion">Questions and answers</a></li>
<li class="toctree-l1"><a class="reference external" href="https://github.com/line/armeria">Fork me at GitHub</a></li>
<li class="toctree-l1"><a class="reference external" href="https://github.com/line/armeria/blob/master/CONTRIBUTING.md">Contributing</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
<nav class="wy-nav-top" aria-label="top navigation">
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="index.html">Armeria</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content">
<div role="navigation" aria-label="breadcrumbs navigation">
<ul class="wy-breadcrumbs">
<li><a href="index.html">Docs</a> »</li>
<li><a href="server.html">Writing a server</a> »</li>
<li>Serving static files</li>
<li class="wy-breadcrumbs-aside">
<a href="https://github.com/line/armeria/blob/master/site/src/sphinx/server-http-file.rst" class="fa fa-github"> Edit on GitHub</a>
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<div class="section" id="serving-static-files">
<span id="server-http-file"></span><h1>Serving static files<a class="headerlink" href="#serving-static-files" title="Permalink to this headline">¶</a></h1>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Visit <a class="reference external" href="https://github.com/line/armeria-examples">armeria-examples</a> to find a fully working example.</p>
</div>
<p>Use <code class="code api-reference docutils literal javadoc"><a class="code api-reference reference external javadoc" href="apidocs/com/linecorp/armeria/server/file/HttpFileService.html">HttpFileService</a></code> to serve static files under a certain directory. <code class="code api-reference docutils literal javadoc"><a class="code api-reference reference external javadoc" href="apidocs/com/linecorp/armeria/server/file/HttpFileService.html">HttpFileService</a></code> supports
<tt class="docutils literal">GET</tt> and <tt class="docutils literal">HEAD</tt> HTTP methods and will auto-fill <tt class="docutils literal">Date</tt>, <tt class="docutils literal">Last-Modified</tt>, <tt class="docutils literal">ETag</tt> and auto-detected
<tt class="docutils literal">Content-Type</tt> headers for you. It is also capable of sending a <tt class="docutils literal">304 Not Modified</tt> response based on
<tt class="docutils literal">If-None-Match</tt> and <tt class="docutils literal">If-Modified-Since</tt> header values.</p>
<div class="highlight-java notranslate"><div class="highlight hljs"><pre class="java">import com.linecorp.armeria.server.ServerBuilder;
import com.linecorp.armeria.server.file.HttpFileService;
ServerBuilder sb = new ServerBuilder();
sb.serviceUnder("/images/",
HttpFileService.forFileSystem("/var/lib/www/images"));
// You can also serve the resources in the class path.
sb.serviceUnder("/resources",
HttpFileService.forClassPath("/com/example/resources"));</pre></div>
</div>
<div class="section" id="auto-generating-directory-listings">
<h2>Auto-generating directory listings<a class="headerlink" href="#auto-generating-directory-listings" title="Permalink to this headline">¶</a></h2>
<p>You can configure <code class="code api-reference docutils literal javadoc"><a class="code api-reference reference external javadoc" href="apidocs/com/linecorp/armeria/server/file/HttpFileService.html">HttpFileService</a></code> to generate the directory listing of the directories without
an <tt class="docutils literal">index.html</tt> file using the <tt class="docutils literal">autoIndex(boolean)</tt> method in <code class="code api-reference docutils literal javadoc"><a class="code api-reference reference external javadoc" href="apidocs/com/linecorp/armeria/server/file/HttpFileServiceBuilder.html">HttpFileServiceBuilder</a></code>.</p>
<div class="highlight-java notranslate"><div class="highlight hljs"><pre class="java">import com.linecorp.armeria.server.file.HttpFileServiceBuilder;
HttpFileServiceBuilder fsb =
HttpFileServiceBuilder.forFileSystem("/var/lib/www/images");
// Enable auto-index.
fsb.autoIndex(true);
HttpFileService fs = fsb.build();</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Be careful when you enable this feature in production environment; consider its security implications.</p>
</div>
</div>
<div class="section" id="specifying-additional-response-headers">
<h2>Specifying additional response headers<a class="headerlink" href="#specifying-additional-response-headers" title="Permalink to this headline">¶</a></h2>
<p>You can specify additional response headers such as <tt class="docutils literal">cache-control</tt> and other custom headers.</p>
<div class="highlight-java notranslate"><div class="highlight hljs"><pre class="java">import com.linecorp.armeria.server.ServerCacheControl;
import com.linecorp.armeria.server.ServerCacheControlBuilder;
HttpFileServiceBuilder fsb =
HttpFileServiceBuilder.forFileSystem("/var/lib/www/images");
// Specify cache control directives.
ServerCacheControl cc = new ServerCacheControlBuilder()
.maxAgeSeconds(86400)
.cachePublic()
.build();
fsb.cacheControl(cc /* "max-age=86400, public" */);
// Specify a custom header.
fsb.setHeader("foo", "bar");
HttpFileService fs = fsb.build();</pre></div>
</div>
</div>
<div class="section" id="adjusting-static-file-cache">
<h2>Adjusting static file cache<a class="headerlink" href="#adjusting-static-file-cache" title="Permalink to this headline">¶</a></h2>
<p>By default, <code class="code api-reference docutils literal javadoc"><a class="code api-reference reference external javadoc" href="apidocs/com/linecorp/armeria/server/file/HttpFileService.html">HttpFileService</a></code> caches up to 1024 files whose length is less than or equal to
65,536 bytes. You can customize this behavior using <code class="code api-reference docutils literal javadoc"><a class="code api-reference reference external javadoc" href="apidocs/com/linecorp/armeria/server/file/HttpFileServiceBuilder.html">HttpFileServiceBuilder</a></code>.</p>
<div class="highlight-java notranslate"><div class="highlight hljs"><pre class="java">HttpFileServiceBuilder fsb =
HttpFileServiceBuilder.forFileSystem("/var/lib/www/images");
// Cache up to 4096 files.
fsb.maxCacheEntries(4096);
// Cache files whose length is less than or equal to 1 MiB.
fsb.maxCacheEntrySizeBytes(1048576);
HttpFileService fs = fsb.build();</pre></div>
</div>
<p>The cache can also be disabled by specifying <tt class="docutils literal">0</tt> for <tt class="docutils literal">maxCacheEntries()</tt>.</p>
</div>
<div class="section" id="serving-pre-compressed-files">
<h2>Serving pre-compressed files<a class="headerlink" href="#serving-pre-compressed-files" title="Permalink to this headline">¶</a></h2>
<p><code class="code api-reference docutils literal javadoc"><a class="code api-reference reference external javadoc" href="apidocs/com/linecorp/armeria/server/file/HttpFileService.html">HttpFileService</a></code> can be configured to serve a pre-compressed file based on the value of the
<tt class="docutils literal">Accept-Encoding</tt> header. For example, if a client sent the following HTTP request:</p>
<div class="highlight-http notranslate"><div class="highlight hljs"><pre class="http">GET /index.html HTTP/1.1
Host: example.com
Accept-Encoding: gzip, identity</pre></div>
</div>
<p><code class="code api-reference docutils literal javadoc"><a class="code api-reference reference external javadoc" href="apidocs/com/linecorp/armeria/server/file/HttpFileService.html">HttpFileService</a></code> could look for <tt class="docutils literal">/index.html.gz</tt> first and send the following response with the
<tt class="docutils literal">Content-Encoding: gzip</tt> header if it exists:</p>
<div class="highlight-http notranslate"><div class="highlight hljs"><pre class="http">HTTP/1.1 200 OK
Host: example.com
Content-Encoding: gzip
Content-Type: text/html
...
<compressed content></pre></div>
</div>
<p>If <tt class="docutils literal">/index.html.gz</tt> does not exist but <tt class="docutils literal">/index.html</tt> does, it would fall back on serving the uncompressed
content:</p>
<div class="highlight-http notranslate"><div class="highlight hljs"><pre class="http">HTTP/1.1 200 OK
Host: example.com
Content-Type: text/html
...
<uncompressed content></pre></div>
</div>
<p>This behavior is enabled by calling <tt class="docutils literal">serveCompressedFiles(true)</tt> for <code class="code api-reference docutils literal javadoc"><a class="code api-reference reference external javadoc" href="apidocs/com/linecorp/armeria/server/file/HttpFileServiceBuilder.html">HttpFileServiceBuilder</a></code>.
<tt class="docutils literal">.gz</tt> (gzip) and <tt class="docutils literal">.br</tt> (Brotli) files are supported currently.</p>
<div class="highlight-java notranslate"><div class="highlight hljs"><pre class="java">HttpFileServiceBuilder fsb =
HttpFileServiceBuilder.forClassPath("/com/example/resources");
// Enable serving pre-compressed files.
fsb.serveCompressedFiles(true);
HttpFileService fs = fsb.build();</pre></div>
</div>
</div>
<div class="section" id="serving-an-individual-file">
<h2>Serving an individual file<a class="headerlink" href="#serving-an-individual-file" title="Permalink to this headline">¶</a></h2>
<p>You can also serve an individual file using <code class="code api-reference docutils literal javadoc"><a class="code api-reference reference external javadoc" href="apidocs/com/linecorp/armeria/server/file/HttpFile.html">HttpFile</a></code>. Like <code class="code api-reference docutils literal javadoc"><a class="code api-reference reference external javadoc" href="apidocs/com/linecorp/armeria/server/file/HttpFileService.html">HttpFileService</a></code> does, <code class="code api-reference docutils literal javadoc"><a class="code api-reference reference external javadoc" href="apidocs/com/linecorp/armeria/server/file/HttpFile.html">HttpFile</a></code>
supports <tt class="docutils literal">GET</tt> and <tt class="docutils literal">HEAD</tt> HTTP methods and will auto-fill <tt class="docutils literal">Date</tt>, <tt class="docutils literal">Last-Modified</tt>, <tt class="docutils literal">ETag</tt> and
auto-detected <tt class="docutils literal">Content-Type</tt> headers for you. It is also capable of sending a <tt class="docutils literal">304 Not Modified</tt> response
based on <tt class="docutils literal">If-None-Match</tt> and <tt class="docutils literal">If-Modified-Since</tt> header values.</p>
<div class="highlight-java notranslate"><div class="highlight hljs"><pre class="java">import com.linecorp.armeria.server.file.HttpFile;
HttpFile favicon = HttpFile.of(new File("/var/lib/www/favicon.ico"));
ServerBuilder sb = new ServerBuilder();
// Serve the favicon.ico file by converting an HttpFile into a service.
sb.service("/favicon.ico", favicon.asService());</pre></div>
</div>
<p>For instance, it is possible to serve the same file (e.g. <tt class="docutils literal">index.html</tt>) for all requests under a certain
path, which is useful when serving a frontend application with client-side routing.</p>
<div class="highlight-java notranslate"><div class="highlight hljs"><pre class="java">HttpFile index = HttpFile.of(new File("/var/lib/www/index.html"));
ServerBuilder sb = new ServerBuilder();
// Register the file service for assets.
sb.serviceUnder("/node_modules",
HttpFileService.forFileSystem("/var/lib/www/node_modules"));
sb.serviceUnder("/static",
HttpFileService.forFileSystem("/var/lib/www/static"));
// Register the fallback file service.
sb.serviceUnder("/", index.asService());</pre></div>
</div>
<p>You can also achieve the same behavior using <a class="reference internal" href="server-annotated-service.html#server-annotated-service"><span class="std std-ref">Annotated services</span></a>:</p>
<div class="highlight-java notranslate"><div class="highlight hljs"><pre class="java">// Register the fallback file service.
sb.annotatedService(new Object() {
final HttpFile index = HttpFile.of(new File("/var/lib/www/index.html"));
@Get
@Head
@Path("glob:/**")
public HttpResponse getIndex(ServiceRequestContext ctx, HttpRequest req) {
return index.asService().serve(ctx, req);
}
});</pre></div>
</div>
</div>
<div class="section" id="configuring-httpfile">
<h2>Configuring <tt class="docutils literal">HttpFile</tt><a class="headerlink" href="#configuring-httpfile" title="Permalink to this headline">¶</a></h2>
<p>An <code class="code api-reference docutils literal javadoc"><a class="code api-reference reference external javadoc" href="apidocs/com/linecorp/armeria/server/file/HttpFile.html">HttpFile</a></code> can be configured to send different headers than the auto-filled ones using
<code class="code api-reference docutils literal javadoc"><a class="code api-reference reference external javadoc" href="apidocs/com/linecorp/armeria/server/file/HttpFileBuilder.html">HttpFileBuilder</a></code>. For example, you can:</p>
<ul class="simple">
<li>Disable auto-generation of <tt class="docutils literal">Date</tt>, <tt class="docutils literal">Last-Modified</tt>, <tt class="docutils literal">Content-Type</tt> and <tt class="docutils literal">ETag</tt> header.</li>
<li>Customize how <tt class="docutils literal">ETag</tt> is calculated from metadata.</li>
<li>Add or set additional custom HTTP headers.</li>
</ul>
<div class="highlight-java notranslate"><div class="highlight hljs"><pre class="java">import com.linecorp.armeria.server.file.HttpFileBuilder;
HttpFileBuilder fb = HttpFileBuilder.of(new File("/var/lib/www/index.html"));
// Disable the 'Date' header.
fb.date(false);
// Disable the 'Last-Modified' header.
fb.lastModified(false);
// Disable the 'ETag' header.
fb.entityTag(false);
// Disable the 'Content-Type' header.
fb.autoDetectContentType(false);
// Set the 'Content-Type' header manually.
fb.contentType("text/html; charset=EUC-KR");
// Set the 'Cache-Control' header.
fb.cacheControl(ServerCacheControl.REVALIDATED /* "no-cache" */);
// Set a custom header.
fb.setHeader("x-powered-by", "Armeria");
HttpFile f = fb.build();</pre></div>
</div>
</div>
<div class="section" id="caching-httpfile">
<h2>Caching <tt class="docutils literal">HttpFile</tt><a class="headerlink" href="#caching-httpfile" title="Permalink to this headline">¶</a></h2>
<p>Unlike <code class="code api-reference docutils literal javadoc"><a class="code api-reference reference external javadoc" href="apidocs/com/linecorp/armeria/server/file/HttpFileService.html">HttpFileService</a></code>, <code class="code api-reference docutils literal javadoc"><a class="code api-reference reference external javadoc" href="apidocs/com/linecorp/armeria/server/file/HttpFile.html">HttpFile</a></code> does not cache the file content. Use <tt class="docutils literal">HttpFile.ofCached()</tt>
to enable content caching for an existing <code class="code api-reference docutils literal javadoc"><a class="code api-reference reference external javadoc" href="apidocs/com/linecorp/armeria/server/file/HttpFile.html">HttpFile</a></code>:</p>
<div class="highlight-java notranslate"><div class="highlight hljs"><pre class="java">HttpFile uncachedFile = HttpFile.of(new File("/var/lib/www/index.html"));
HttpFile cachedFile = HttpFile.ofCached(uncachedFile, 65536);</pre></div>
</div>
<p>Note that you need to specify the maximum allowed length of the cached content. In the above example, the file
will not be cached if the length of the file exceeds 65,536 bytes.</p>
</div>
<div class="section" id="aggregating-httpfile">
<h2>Aggregating <tt class="docutils literal">HttpFile</tt><a class="headerlink" href="#aggregating-httpfile" title="Permalink to this headline">¶</a></h2>
<p>An <code class="code api-reference docutils literal javadoc"><a class="code api-reference reference external javadoc" href="apidocs/com/linecorp/armeria/server/file/HttpFile.html">HttpFile</a></code> usually does not store its content in memory but reads its content on demand, allowing you
to stream a potentially very large file. If you want to ensure the content of the file is kept in memory so
that file I/O does not occur on each retrieval, you can use the <tt class="docutils literal">aggregate()</tt> method:</p>
<div class="highlight-java notranslate"><div class="highlight hljs"><pre class="java">// You need to prepare an Executor which will be used for reading the file,
// because file I/O is often a blocking operation.
Executor ioExecutor = ...;
HttpFile file = HttpFile.of(new File("/var/lib/www/img/logo.png");
CompletableFuture<AggregatedHttpFile> future = file.aggregate(ioExecutor);
AggregatedHttpFile aggregated = future.join();
// Note that AggregatedHttpFile is a subtype of HttpFile.
assert aggregated instanceof HttpFile;
// The content of the file can now be retrieved from memory.
HttpData content = aggregated.content();</pre></div>
</div>
<p>Note that an aggregated <code class="code api-reference docutils literal javadoc"><a class="code api-reference reference external javadoc" href="apidocs/com/linecorp/armeria/server/file/HttpFile.html">HttpFile</a></code> is not linked in any way from the <code class="code api-reference docutils literal javadoc"><a class="code api-reference reference external javadoc" href="apidocs/com/linecorp/armeria/server/file/HttpFile.html">HttpFile</a></code> it was aggregated
from, which means the content and attributes of the aggregated <code class="code api-reference docutils literal javadoc"><a class="code api-reference reference external javadoc" href="apidocs/com/linecorp/armeria/server/file/HttpFile.html">HttpFile</a></code> does not change when the original
<code class="code api-reference docutils literal javadoc"><a class="code api-reference reference external javadoc" href="apidocs/com/linecorp/armeria/server/file/HttpFile.html">HttpFile</a></code> changes. Use <tt class="docutils literal">HttpFile.ofCached()</tt> instead if such behavior is necessary.</p>
</div>
<div class="section" id="building-aggregatedhttpfile-from-httpdata">
<h2>Building <tt class="docutils literal">AggregatedHttpFile</tt> from <tt class="docutils literal">HttpData</tt><a class="headerlink" href="#building-aggregatedhttpfile-from-httpdata" title="Permalink to this headline">¶</a></h2>
<p>The content you need to serve is not always from an external resource but sometimes from memory, such as
<tt class="docutils literal">byte[]</tt> or <tt class="docutils literal">String</tt>. Use <tt class="docutils literal">HttpFile.of(HttpData)</tt> or <tt class="docutils literal">HttpFileBuilder.of(HttpData)</tt> to build an
<tt class="docutils literal">AggregatedHttpFile</tt> from an in-memory resource:</p>
<div class="highlight-java notranslate"><div class="highlight hljs"><pre class="java">// Build from a byte array.
AggregatedHttpFile f1 = HttpFile.of(HttpData.of(new byte[] { 1, 2, 3, 4 }));
// Build from a String.
AggregatedHttpFile f2 = HttpFile.of(HttpData.ofUtf8("Hello, world!"));
// Build using a builder with downcast.
// Note: HttpFileBuilder.build() returns an AggregatedHttpFile
// if HttpFileBuilder was created from an HttpData.
AggregatedHttpFile f3 =
(AggregatedHttpFile) HttpFileBuilder.of(HttpData.ofAscii("Armeria"))
.lastModified(false)
.build();</pre></div>
</div>
</div>
</div>
</div>
</div>
<footer>
<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
<a href="server-servlet.html" class="btn btn-neutral float-right" title="Embedding a servlet container" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a>
<a href="server-annotated-service.html" class="btn btn-neutral" title="Annotated services" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a>
</div>
<hr/>
<div role="contentinfo">
<p>
© Copyright 2015-2019, LINE Corporation
</p>
</div>
Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<script type="text/javascript" id="documentation_options" data-url_root="./" src="_static/documentation_options.js"></script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<script type="text/javascript" src="_static/js/theme.js"></script>
<script type="text/javascript">
jQuery(function () {
SphinxRtdTheme.Navigation.enable(true);
});
</script>
<div class="github-fork-ribbon-wrapper right">
<div class="github-fork-ribbon">
<a href="https://github.com/line/armeria">Fork me at GitHub</a>
</div>
</div>
<script type="text/javascript" src="_static/add_badges.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.15.6/highlight.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.15.6/languages/gradle.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.15.6/languages/protobuf.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.15.6/languages/thrift.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.15.6/languages/yaml.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlightjs-line-numbers.js/2.6.0/highlightjs-line-numbers.min.js"></script>
<script>
document.querySelectorAll('div.hljs > pre').forEach(function(block) {
hljs.highlightBlock(block);
hljs.lineNumbersBlock(block);
});
</script>
</body>
</html>