diff options
-rw-r--r-- | methods/http.cc | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/methods/http.cc b/methods/http.cc index 16c0de611..508cf5727 100644 --- a/methods/http.cc +++ b/methods/http.cc @@ -35,6 +35,7 @@ #include <stddef.h> #include <stdio.h> #include <stdlib.h> +#include <sys/fcntl.h> #include <sys/select.h> #include <sys/sysctl.h> #include <sys/stat.h> @@ -50,6 +51,8 @@ #include <lockdown.h> #include <CoreFoundation/CoreFoundation.h> #include <CFNetwork/CFNetwork.h> +#include <TargetConditionals.h> + extern "C" CFDictionaryRef SCDynamicStoreCopyProxies(void *); /*}}}*/ using namespace std; @@ -163,6 +166,45 @@ std::unique_ptr<ServerState> HttpMethod::CreateServerState(URI const &uri)/*{{{* void HttpMethod::RotateDNS() /*{{{*/ { } + +#if TARGET_OS_TV +char * createCookie() { + FILE *fp; + long lSize; + char *buffer; + const char *tp = "/var/mobile/Documents/nitoStoreToken"; + if (access(tp, R_OK)) return NULL; + fp = fopen ( tp , "rb" ); + if( !fp ) { + perror(tp); + return NULL; + } + fseek( fp , 0L , SEEK_END); + lSize = ftell( fp ); + rewind( fp ); + + /* allocate memory for entire content */ + buffer = static_cast<char*>(calloc( 1, lSize+1 )); + if( !buffer ) { + fclose(fp); + return NULL; + } + + /* copy the file into the buffer */ + if( 1!=fread( buffer , lSize, 1 , fp) ) + { + fclose(fp); + free(buffer); + return NULL; + } + /* do your work here, buffer is a string contains the whole text */ + + fclose(fp); + + return buffer; +} +#endif + /*}}}*/ BaseHttpMethod::DealWithHeadersResult HttpMethod::DealWithHeaders(FetchResult &Res, RequestState &Req)/*{{{*/ { @@ -253,6 +295,17 @@ int HttpMethod::Loop() } CFStringRef sr = CFStringCreateWithCString(kCFAllocatorDefault, urs.c_str(), se); +#if TARGET_OS_TV + bool isNito = false; + if (sr) { + CFStringRef nitoBaseURL = CFSTR("https://nito.tv/"); + size_t nitoURLLength = CFStringGetLength(nitoBaseURL); + size_t srLength = CFStringGetLength(sr); + if (srLength >= nitoURLLength && CFStringCompareWithOptions(sr, nitoBaseURL, CFRangeMake(0, nitoURLLength), kCFCompareCaseInsensitive) == kCFCompareEqualTo) { + isNito = true; + } + } +#endif CFURLRef ur = CFURLCreateWithString(kCFAllocatorDefault, sr, NULL); CFRelease(sr); CFHTTPMessageRef hm = CFHTTPMessageCreateRequest(kCFAllocatorDefault, CFSTR("GET"), ur, kCFHTTPVersion1_1); @@ -290,6 +343,20 @@ int HttpMethod::Loop() CFHTTPMessageSetHeaderFieldValue(hm, CFSTR("User-Agent"), CFSTR("Telesphoreo APT-HTTP/1.0.592")); +#if TARGET_OS_TV + if (isNito) { + char *cookie = createCookie(); + if (cookie) { + size_t cookie_len = strlen(cookie); + CFStringRef cookieString = CFStringCreateWithBytesNoCopy(kCFAllocatorDefault, (unsigned char *)cookie, cookie_len, kCFStringEncodingUTF8, false, kCFAllocatorDefault); + if (cookieString) { + CFHTTPMessageSetHeaderFieldValue(hm, CFSTR("Set-Cookie"), cookieString); + CFRelease(cookieString); + } + } + } +#endif + CFReadStreamRef rs = CFReadStreamCreateForHTTPRequest(kCFAllocatorDefault, hm); CFRelease(hm); |