编写下载服务器。 第四部分:有效地实现HEAD操作
HEAD是一個經常被遺忘的HTTP方法(動詞),其行為類似于GET,但不會返回正文。 您使用HEAD來檢查資源的存在(如果不存在,它應該返回404),并確保您的緩存中沒有陳舊的版本。 在這種情況下,您期望304 Not Modified ,而200表示服務器具有最新版本。 例如,您可以使用HEAD有效地實施軟件更新。 在這種情況下, ETag是您的應用程序版本(生成,標記,提交哈希),并且您具有固定的/most_recent端點。 您的軟件會在ETag發送具有當前版本的HEAD請求。 如果沒有更新,服務器將以304答復。如果為200,則可以詢問用戶是否要升級而無需下載軟件。 最終請求GET /most_recent將始終下載您軟件的最新版本。 HTTP的力量!
在servlet中,默認情況下, HEAD是在doHead() ,您應該重寫它。 默認實現只是將委派給GET但會丟棄主體。 這效率不高,尤其是當您從外部(例如Amazon S3)加載資源時。 幸運的是(?)Spring MVC默認情況下不實現HEAD,因此您必須手動執行。 讓我們從HEAD的一些集成測試開始:
def 'should return 200 OK on HEAD request, but without body'() {expect:mockMvc.perform(head('/download/' + FileExamples.TXT_FILE_UUID)).andExpect(status().isOk()).andExpect(content().bytes(new byte[0])) }def 'should return 304 on HEAD request if we have cached version'() {expect:mockMvc.perform(head('/download/' + FileExamples.TXT_FILE_UUID).header(IF_NONE_MATCH, FileExamples.TXT_FILE.getEtag())).andExpect(status().isNotModified()).andExpect(header().string(ETAG, FileExamples.TXT_FILE.getEtag())) }def 'should return Content-length header'() {expect:mockMvc.perform(head('/download/' + FileExamples.TXT_FILE_UUID)).andExpect(status().isOk()).andExpect(header().longValue(CONTENT_LENGTH, FileExamples.TXT_FILE.size)) }實際的實現非常簡單,但是需要一些重構以避免重復。 下載端點現在接受GET和HEAD:
@RequestMapping(method = {GET, HEAD}, value = "/{uuid}") public ResponseEntity<Resource> download(HttpMethod method,@PathVariable UUID uuid,@RequestHeader(IF_NONE_MATCH) Optional<String> requestEtagOpt,@RequestHeader(IF_MODIFIED_SINCE) Optional<Date> ifModifiedSinceOpt) {return storage.findFile(uuid).map(pointer -> new ExistingFile(method, pointer)).map(file -> file.handle(requestEtagOpt, ifModifiedSinceOpt)).orElseGet(() -> new ResponseEntity<>(NOT_FOUND)); }我創建了一個新的抽象ExistingFile ,它封裝了我們在其上調用的找到的FilePointer和HTTP動詞。 ExistingFile.handle()具有通過HEAD提供文件或僅提供元數據所需的一切:
public class ExistingFile {private static final Logger log = LoggerFactory.getLogger(ExistingFile.class);private final HttpMethod method;private final FilePointer filePointer;public ExistingFile(HttpMethod method, FilePointer filePointer) {this.method = method;this.filePointer = filePointer;}public ResponseEntity<Resource> handle(Optional<String> requestEtagOpt, Optional<Date> ifModifiedSinceOpt) {if (requestEtagOpt.isPresent()) {final String requestEtag = requestEtagOpt.get();if (filePointer.matchesEtag(requestEtag)) {return notModified(filePointer);}}if (ifModifiedSinceOpt.isPresent()) {final Instant isModifiedSince = ifModifiedSinceOpt.get().toInstant();if (filePointer.modifiedAfter(isModifiedSince)) {return notModified(filePointer);}}return serveDownload(filePointer);}private ResponseEntity<Resource> serveDownload(FilePointer filePointer) {log.debug("Serving {} '{}'", method, filePointer);final InputStreamResource resource = resourceToReturn(filePointer);return response(filePointer, OK, resource);}private InputStreamResource resourceToReturn(FilePointer filePointer) {if (method == HttpMethod.GET)return buildResource(filePointer);elsereturn null;}private InputStreamResource buildResource(FilePointer filePointer) {final InputStream inputStream = filePointer.open();return new InputStreamResource(inputStream);}private ResponseEntity<Resource> notModified(FilePointer filePointer) {log.trace("Cached on client side {}, returning 304", filePointer);return response(filePointer, NOT_MODIFIED, null);}private ResponseEntity<Resource> response(FilePointer filePointer, HttpStatus status, Resource body) {return ResponseEntity.status(status).eTag(filePointer.getEtag()).lastModified(filePointer.getLastModified().toEpochMilli()).body(body);}}resourceToReturn()至關重要。 如果返回null ,Spring MVC將不包含任何主體作為響應。 其他所有內容均保持不變(響應標頭等)
編寫下載服務器
- 第一部分:始終流式傳輸,永遠不要完全保留在內存中
- 第二部分:標頭:Last-Modified,ETag和If-None-Match
- 第三部分:標頭:內容長度和范圍
- 第四部分:有效地實現HEAD操作
- 第五部分:油門下載速度
- 第六部分:描述您發送的內容(內容類型等)
- 這些文章中開發的示例應用程序可在GitHub上找到。
翻譯自: https://www.javacodegeeks.com/2015/07/writing-a-download-server-part-iv-implement-head-operation-efficiently.html
總結
以上是生活随笔為你收集整理的编写下载服务器。 第四部分:有效地实现HEAD操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PayPal提现被退回的解决办法?(教程
- 下一篇: 龙芯3A5000LL与i7-10700的