summaryrefslogtreecommitdiff
path: root/data/_apt1.8/apt_nito.diff
blob: eec3b1b61ace9243c8a29ea9dfc167d7d6f859ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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);