xref: /haiku/src/apps/haikudepot/server/AbstractServerProcess.h (revision ed24eb5ff12640d052171c6a7feba37fab8a75d1)
1 /*
2  * Copyright 2017-2020, Andrew Lindesay <apl@lindesay.co.nz>.
3  * All rights reserved. Distributed under the terms of the MIT License.
4  */
5 #ifndef ABSTRACT_SERVER_PROCESS_H
6 #define ABSTRACT_SERVER_PROCESS_H
7 
8 #include <HttpRequest.h>
9 #include <Json.h>
10 #include <String.h>
11 #include <Url.h>
12 
13 #include "AbstractProcess.h"
14 #include "StandardMetaData.h"
15 
16 
17 using BPrivate::Network::BHttpRequest;
18 
19 
20 typedef enum server_process_options {
21 	SERVER_PROCESS_NO_NETWORKING	= 1 << 0,
22 	SERVER_PROCESS_PREFER_CACHE		= 1 << 1,
23 	SERVER_PROCESS_DROP_CACHE		= 1 << 2
24 } server_process_options;
25 
26 
27 /*! This is the superclass of Processes that communicate with the Haiku Depot
28     Server (HDS) system.
29 */
30 
31 
32 class AbstractServerProcess : public AbstractProcess {
33 public:
34 								AbstractServerProcess(uint32 options);
35 	virtual						~AbstractServerProcess();
36 
37 protected:
38 	virtual	status_t			GetStandardMetaDataPath(
39 									BPath& path) const = 0;
40 	virtual	void				GetStandardMetaDataJsonPath(
41 									BString& jsonPath) const = 0;
42 
43 	virtual	status_t			IfModifiedSinceHeaderValue(
44 									BString& headerValue) const;
45 			status_t			IfModifiedSinceHeaderValue(
46 									BString& headerValue,
47 									const BPath& metaDataPath,
48 									const BString& jsonPath) const;
49 	static	void				SetIfModifiedSinceHeaderValueFromMetaData(
50 									BString& headerValue,
51 									const StandardMetaData& metaData);
52 
53 			status_t			PopulateMetaData(
54 									StandardMetaData& metaData,
55 									const BPath& path,
56 									const BString& jsonPath) const;
57 
58 			status_t			ParseJsonFromFileWithListener(
59 									BJsonEventListener *listener,
60 									const BPath& path) const;
61 
62 	virtual	status_t			DownloadToLocalFileAtomically(
63 									const BPath& targetFilePath,
64 									const BUrl& url);
65 
66 			status_t			DeleteLocalFile(const BPath& filePath);
67 			status_t			MoveDamagedFileAside(const BPath& filePath);
68 
69 			bool				HasOption(uint32 flag);
70 			bool				ShouldAttemptNetworkDownload(
71 									bool hasDataAlready);
72 
73 	static	bool				IsSuccess(status_t e);
74 
75 protected:
76 	virtual status_t			StopInternal();
77 
78 private:
79 			status_t			DownloadToLocalFile(
80 									const BPath& targetFilePath,
81 									const BUrl& url,
82 									uint32 redirects, uint32 failures);
83 
84 	static bool					_LooksLikeGzip(const char *pathStr);
85 	static status_t				_DeGzipInSitu(const BPath& path);
86 
87 private:
88 			uint32				fOptions;
89 
90 			BHttpRequest*		fRequest;
91 };
92 
93 #endif // ABSTRACT_SERVER_PROCESS_H
94