GithubClient.cpp 2.7 KB

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