|
| 1 | +/* |
| 2 | +Copyright (C) 2015 by Joachim Meyer |
| 3 | +
|
| 4 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +of this software and associated documentation files (the "Software"), to deal |
| 6 | +in the Software without restriction, including without limitation the rights |
| 7 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | +copies of the Software, and to permit persons to whom the Software is |
| 9 | +furnished to do so, subject to the following conditions: |
| 10 | +
|
| 11 | +The above copyright notice and this permission notice shall be included in |
| 12 | +all copies or substantial portions of the Software. |
| 13 | +
|
| 14 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | +THE SOFTWARE. |
| 21 | +*/ |
| 22 | + |
| 23 | +#pragma once |
| 24 | +#include <string.h> |
| 25 | + |
| 26 | +#include "PolyGlobals.h" |
| 27 | +#include "PolyThreaded.h" |
| 28 | + |
| 29 | +#define HTTP_VERSION "HTTP/1.1" |
| 30 | +#define DEFAULT_USER_AGENT "Polycode HTTP Fetcher/1.0" |
| 31 | +#define DEFAULT_PAGE_BUF_SIZE 2048 |
| 32 | + |
| 33 | +namespace Polycode { |
| 34 | + |
| 35 | + class HTTPFetcherEvent : public Event { |
| 36 | + public: |
| 37 | + HTTPFetcherEvent() { contentSize = 0; errorCode = 0; data = NULL; storedInFile = false; } |
| 38 | + ~HTTPFetcherEvent(){} |
| 39 | + |
| 40 | + //If storedInFile: data is the file path, else: data contains all the fetched data |
| 41 | + char* data; |
| 42 | + //Error code: contains either the errno / WSAError code or the HTTP error code or the HTTPFetcher error code |
| 43 | + int errorCode; |
| 44 | + |
| 45 | + //Has the data been saved to a file or is it shipped with this event? |
| 46 | + bool storedInFile; |
| 47 | + //Size of the HTTP reply |
| 48 | + unsigned long contentSize; |
| 49 | + |
| 50 | + static const int EVENTBASE_SOCKETEVENT = 0x500; |
| 51 | + static const int EVENT_HTTP_ERROR = EVENTBASE_SOCKETEVENT + 2; |
| 52 | + static const int EVENT_HTTP_DATA_RECEIVED = EVENTBASE_SOCKETEVENT + 3; |
| 53 | + }; |
| 54 | + |
| 55 | + /** |
| 56 | + * A utility to download a file from the WWW through HTTP. It is threaded (and therefor non blocking). |
| 57 | + * If you want to use the data you might add an EventListener for the HTTPFetcherEvent::EVENT_HTTP_DATA_RECEIVED event code. |
| 58 | + */ |
| 59 | + class HTTPFetcher : public Threaded { |
| 60 | + public: |
| 61 | + /* |
| 62 | + * Connects to a host and fetches a file given in the param |
| 63 | + * @param address Full path including the hostname (Domain or IP) and protocol (http://) aswell as the path to the file on the server |
| 64 | + * @param saveToPath true if you want the file to be directly saved, false if you just want the data as char array |
| 65 | + * @param savePath Path String where the file should be saved to |
| 66 | + */ |
| 67 | + HTTPFetcher(String address, bool saveToPath = false, String savePath = ""); |
| 68 | + ~HTTPFetcher(); |
| 69 | + |
| 70 | + String getData(); |
| 71 | + |
| 72 | + /* |
| 73 | + * Fetches a file given in the param |
| 74 | + * @param pathToFile Path String to the new file to fetch from the same host. Without leading "/" |
| 75 | + * @param saveToPath true if you want the file to be directly saved, false if you just want the data as char array |
| 76 | + * @param savePath Path String where the file should be saved to |
| 77 | + */ |
| 78 | + void fetchFile(String pathToFile, bool saveToPath = false, String savePath = ""); |
| 79 | + |
| 80 | + //The received data is more or less than the HTTP header told us it should be |
| 81 | + static const int HTTPFETCHER_ERROR_WRONG_SIZE = 0x10F00; |
| 82 | + |
| 83 | + bool storeInFile; |
| 84 | + |
| 85 | + private: |
| 86 | + int s; |
| 87 | + String address; |
| 88 | + String bodyReturn; |
| 89 | + String path; |
| 90 | + String host; |
| 91 | + String protocol; |
| 92 | + String savePath; |
| 93 | + |
| 94 | + bool createSocket(); |
| 95 | + void updateThread(); |
| 96 | + }; |
| 97 | +} |
0 commit comments