GithubClient.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include <GithubClient.h>
  2. #include <FS.h>
  3. #include <Size.h>
  4. static const char HTTP_REQUEST_FORMAT[] PROGMEM =
  5. "GET %s HTTP/1.1\r\nHost: %s\r\nUser-Agent: esp8266_milight_hub\r\nConnection: close\r\n\r\n";
  6. Stream& GithubClient::stream(const String& path) {
  7. if (!client.connect(domain.c_str(), 443)) {
  8. Serial.println(F("Failed to connect to github over HTTPS."));
  9. return client;
  10. }
  11. char buffer[strlen_P(HTTP_REQUEST_FORMAT)+1];
  12. strcpy_P(buffer, HTTP_REQUEST_FORMAT);
  13. client.printf(
  14. buffer,
  15. path.c_str(),
  16. domain.c_str()
  17. );
  18. return client;
  19. }
  20. bool GithubClient::download(const String& path, Stream& dest) {
  21. Stream& client = stream(path);
  22. if (client.available()) {
  23. if (!client.find("\r\n\r\n")) {
  24. Serial.println(F("Error seeking to body"));
  25. return false;
  26. }
  27. } else {
  28. Serial.println(F("Failed to open stream to Github"));
  29. return false;
  30. }
  31. Serial.println(F("Downloading..."));
  32. size_t bytes = 0;
  33. size_t nextCheckpoint = 4096;
  34. while (client.available()) {
  35. size_t l = client.readBytes(buffer, GITHUB_CLIENT_BUFFER_SIZE);
  36. size_t w = dest.write(buffer, l);
  37. dest.flush();
  38. if (w != l) {
  39. printf_P(PSTR("Error writing to stream. Expected to write %d bytes, but only wrote %d\n"), l, w);
  40. return false;
  41. }
  42. bytes += w;
  43. if (bytes % 10 == 0) {
  44. printf_P(".");
  45. }
  46. if (bytes >= nextCheckpoint) {
  47. printf("[%d KB]\n", bytes/1024);
  48. nextCheckpoint += 4096;
  49. }
  50. yield();
  51. }
  52. Serial.println(F("\n"));
  53. return true;
  54. }
  55. bool GithubClient::download(const String& path, const String& fsPath) {
  56. String tmpFile = fsPath + ".download_tmp";
  57. File f = SPIFFS.open(tmpFile.c_str(), "w");
  58. if (!f) {
  59. Serial.print(F("ERROR - could not open file for downloading: "));
  60. Serial.println(fsPath);
  61. return false;
  62. }
  63. printf(".");
  64. if (!download(path, f)) {
  65. f.close();
  66. return false;
  67. }
  68. f.flush();
  69. f.close();
  70. SPIFFS.remove(fsPath);
  71. SPIFFS.rename(tmpFile, fsPath);
  72. printf_P(PSTR("Finished downloading file: %s\n"), fsPath.c_str());
  73. return true;
  74. }
  75. String GithubClient::buildRepoPath(const String& username, const String& repo, const String& repoPath) {
  76. String path = String("/") + username + "/" + repo + "/master/" + repoPath;
  77. return path;
  78. }
  79. String GithubClient::buildApiRequest(const String &username, const String &repo, const String &path) {
  80. return String("/repos/") + username + "/" + repo + path;
  81. }
  82. GithubClient GithubClient::rawDownloader() {
  83. return GithubClient(GITHUB_RAW_DOMAIN);
  84. }
  85. GithubClient GithubClient::apiClient() {
  86. return GithubClient(GITHUB_API_DOMAIN);
  87. }