-
Notifications
You must be signed in to change notification settings - Fork 0
/
myctrl_readwebfile.cpp
261 lines (240 loc) · 8.24 KB
/
myctrl_readwebfile.cpp
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "myctrl_readwebfile.h"
// 1 = wifi net
// 2 = music
// 4 = stream
// 8 = keyboard/mouse move
// 16 = movie
// 32 = searcg
extern int debugmode; // 64 = radio station land icon loader
// 128= stream search
// 256 = tv program stuf
// 512 = media importer
// 1024 = flag loader
// ****************************************************************************************
//
// return string (fname) for filename long = last path + name as filename
//
// ****************************************************************************************
//
int get_webfilenamelong(char *fname,char *webpath) {
char *npointer=NULL;
char *filename; // to save filename
char tmp[20000];
int firstslashpointer=0;
strcpy(tmp,webpath);
npointer=strrchr(tmp,'/');
if (npointer) {
firstslashpointer=npointer-tmp; // husk sted
filename=new char[strlen(npointer)+1];
if (filename) {
strcpy(filename,npointer+1); // save filename
tmp[firstslashpointer-1]='\0';
npointer=strrchr(tmp,'/');
if (npointer) {
//strcpy(fname,filename);
/* old have errors
strcpy(fname,npointer+1);
//fname[11]='\0';
strcat(fname,filename);
*/
strcpy(fname,filename);
}
delete [] filename;
}
return(1);
}
return(0);
}
// ****************************************************************************************
//
// return string for filename or null
//
// ****************************************************************************************
int get_webfilename(char *fname,char *webpath) {
char *npointer=NULL;
npointer=strrchr(webpath,'/');
if (npointer) {
strcpy(fname,npointer+1);
return(1);
}
return(0);
}
// ****************************************************************************************
//
// web downloader ver .1 by hans-henrik
//
// return hostname ,webpath from source link eks. www.dr.dk/images/image1.png
// return hname = www.dr.dk
// return webpath = /images/image1.png
//
// ****************************************************************************************
void get_host(char *hname,char *webpath,char *source) {
int n=0;
int nn=0;
char tegn[5];
char tmptxt[1000];
char tmptxt2[1000];
strcpy(tmptxt2,"");
strcpy(tmptxt,source);
if ((strlen(source)>7) && (strncmp(source,"http://",7)==0)) strcpy(tmptxt,source+7); // remove http(s)://
else if ((strlen(source)>7) && (strncmp(source,"https://",8)==0)) strcpy(tmptxt,source+8); // remove http(s)://
else strcpy(tmptxt,"");
if (strchr(tmptxt,'/')) {
while((tmptxt[n]!='/') && (n<strlen(source))) {
n++;
}
tmptxt[n]=0;
strcpy(hname,tmptxt);
while(n<strlen(source)) {
if (strncmp(source,"http://",7)==0) tmptxt2[nn]=source[n+7];
else if (strncmp(source,"https://",8)==0) tmptxt2[nn]=source[n+8];
else tmptxt2[nn]=source[n];
nn++;
n++;
}
tmptxt2[nn]=0;
strcpy(webpath,tmptxt2);
}
}
// ****************************************************************************************
//
// downloader of file
//
// ****************************************************************************************
int get_webfile(char *webpath,char *outfile) {
struct sockaddr_in servaddr;
struct hostent *hp;
int sock_id;
char message[2024] = {0};
int msglen;
char request[1000];
char *hostpointer;
char hostname[200];
char wpath[200];
char *lpos;
int webobjlength;
int i=0;
bool loaderror=false;
char tegn;
FILE *fil;
int error=0;
strcpy(hostname,"");
// return hostname
// wpath = text after hostbname (path to file - domainname)
get_host(hostname,wpath,webpath); // sample webpage http://www.dr.dk/image
// http 1.1
sprintf(request,"GET %s HTTP/1.1\r\nHost: %s\r\n\r\n",wpath,hostname);
//Get a socket
if((sock_id = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
fprintf(stderr,"Couldn't get a socket.\n");
exit(EXIT_FAILURE);
}
//book uses bzero which my man pages say is deprecated
//the man page said to use memset instead. :-)
memset(&servaddr,0,sizeof(servaddr));
//get address for google.com
if((hp = gethostbyname(hostname)) == NULL) {
fprintf(stderr,"Couldn't get an address.\n");
return(0);
}
//bcopy is deprecated also, using memcpy instead
memcpy((char *)&servaddr.sin_addr.s_addr, (char *)hp->h_addr, hp->h_length);
//fill int port number and type
servaddr.sin_port = htons(80);
servaddr.sin_family = AF_INET;
//make the connection
if(connect(sock_id, (struct sockaddr *)&servaddr, sizeof(servaddr)) != 0) {
fprintf(stderr, "Couldn't connect.\n");
error=1;
loaderror=1;
}
if (!(error)) {
//send the request
send(sock_id,request,strlen(request),0);
//write(sock_id,request,strlen(request));
}
while((strcmp(message,"\r\n")) && (!(loaderror))) {
for(i = 0;strcmp(message + i - 2, "\r\n"); i++) {
read(sock_id,message+i,1);
message[i+1]=0;
}
lpos=strstr(message, "Content-Length:");
if (strstr(message, "Content-Length:")==message) {
webobjlength = atoi(strchr(lpos, ' ') + 1);
}
if (strstr(message, "Invalid URL")) {
webobjlength = 0;
loaderror=true;
}
if (strstr(message,"HTTP/1.0 400 Bad Request")) {
webobjlength = 0;
loaderror=true;
}
if (strstr(message,"302 Found")) {
webobjlength = 0;
loaderror=true;
}
if (strstr(message,"404 Not Found")) {
webobjlength = 0;
loaderror=true;
}
if (strstr(message,"Moved Parmanently")) {
webobjlength = 0;
loaderror=true;
}
}
if (!(loaderror)) {
fil=fopen(outfile,"w");
if (!(fil)) {
if (debugmode) fprintf(stderr," Open file for write error %s \n",outfile);
loaderror=true; // not posible to save file
}
if (fil) {
for (i = 0;i < webobjlength; i++) {
read(sock_id, &tegn, 1);
fputc(tegn, fil);
putchar('\r');
}
fclose(fil);
}
}
close(sock_id);
if (!(loaderror)) return(1); else return(0);
}
// ****************************************************************************************
//
// check if file is a image file of type jpg,png
//
// ****************************************************************************************
bool check_filename_ext(const char *filename) {
const char *dot = strrchr(filename, '.');
if ((dot) && ((strcmp(dot,".jpg")==0) || (strcmp(dot,".png")))) return(1); else return(0);
}
// ****************************************************************************************
//
// used to download images from web
// more secure now use wget
//
// ****************************************************************************************
int get_webfile2(char *webpath,char *outfile) {
char command[2048];
// check file ext is image yes download
if ((check_filename_ext(webpath)) && (strlen(webpath)<300)) {
strcpy(command,"wget \"");
strcat(command,webpath);
strcat(command,"\" -O- | convert -thumbnail 'x320^' - - > ");
strcat(command,outfile);
strcat(command," 2>&1 "); // disable output
//strcat(command," 2>> wget.log ");
//if (debugmode & 4) printf(" do COMMAND *%s* \n",command);
return (system(command));
}
}