PolyHTTPFetcher.h 3.5 KB

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