Subversion Repositories SvarDOS

Rev

Rev 336 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 336 Rev 338
Line 32... Line 32...
32
#include <string.h>
32
#include <string.h>
33
#include <time.h>
33
#include <time.h>
34
 
34
 
35
#include "net.h"
35
#include "net.h"
36
 
36
 
37
#define PVER "20210514"
37
#define PVER "20210520"
38
#define PDATE "2021"
38
#define PDATE "2021"
39
 
39
 
40
#define HOSTADDR "svardos.osdn.io"
40
#define HOSTADDR "svardos.osdn.io"
41
 
41
 
42
 
42
 
43
/* strips http headers and returns new buff len */
43
/* returns length of all http headers, or 0 if uncomplete yet */
44
static int detecthttpheadersend(unsigned char *buff, int len) {
44
static unsigned short detecthttpheadersend(const unsigned char *buff) {
45
  static char lastbyteislf = 0; /* static because I must potentially remember it for next packet/call */
45
  char lastbyteislf = 0;
46
  int i;
46
  unsigned short i;
47
  for (i = 0; i < len; i++) {
47
  for (i = 0; buff[i] != 0; i++) {
48
    if (buff[i] == '\r') continue; /* ignore CR characters */
48
    if (buff[i] == '\r') continue; /* ignore CR characters */
49
    if (buff[i] != '\n') {
49
    if (buff[i] != '\n') {
50
      lastbyteislf = 0;
50
      lastbyteislf = 0;
51
      continue;
51
      continue;
52
    }
52
    }
53
    /* cur byte is LF -> if last one was also LF then this is an empty line, meaning headers are over */
53
    /* cur byte is LF -> if last one was also LF then this is an empty line, meaning headers are over */
54
    if (lastbyteislf == 0) {
54
    if (lastbyteislf == 0) {
55
      lastbyteislf = 1;
55
      lastbyteislf = 1;
56
      continue;
56
      continue;
57
    }
57
    }
58
    /* end of headers! rewind the buffer and return new len */
58
    /* end of headers! return length of headers */
59
    i += 1; /* add 1 to skip the current \n character */
59
    return(i + 1); /* add 1 to skip the current \n character */
60
    len -= i;
-
 
61
    if (len > 0) memmove(buff, buff + i, len + 1); /* +1 so I catch the string terminator as well */
-
 
62
    return(len);
-
 
63
  }
60
  }
64
  return(0);
61
  return(0);
65
}
62
}
66
 
63
 
67
 
64
 
Line 105... Line 102...
105
  }
102
  }
106
  return(0);
103
  return(0);
107
}
104
}
108
 
105
 
109
 
106
 
-
 
107
static int htget_headers(unsigned char *buffer, size_t buffersz, struct net_tcpsocket *sock, int *httpcode)  {
-
 
108
  unsigned char *buffptr = buffer;
-
 
109
  unsigned short bufflen = 0;
-
 
110
  int byteread;
-
 
111
  time_t starttime = time(NULL);
-
 
112
  for (;;) {
-
 
113
    byteread = net_recv(sock, buffptr, buffersz - (bufflen + 1)); /* -1 because I will append a NULL terminator */
-
 
114
 
-
 
115
    if (byteread > 0) { /* got data */
-
 
116
      int hdlen;
-
 
117
      bufflen += byteread;
-
 
118
      buffptr += byteread;
-
 
119
      buffer[bufflen] = 0;
-
 
120
      hdlen = detecthttpheadersend(buffer);
-
 
121
      if (hdlen > 0) { /* full headers - parse http code and continue processing */
-
 
122
        int spc;
-
 
123
        /* find the first space (HTTP/1.1 200 OK) */
-
 
124
        for (spc = 0; spc < 16; spc++) {
-
 
125
          if (buffer[spc] == ' ') break;
-
 
126
          if (buffer[spc] == 0) break;
-
 
127
        }
-
 
128
        if (buffer[spc] != ' ') return(-1);
-
 
129
        *httpcode = atoi((char *)(buffer + spc + 1));
-
 
130
        /* rewind the buffer */
-
 
131
        bufflen -= hdlen;
-
 
132
        memmove(buffer, buffer + hdlen, bufflen);
-
 
133
        return(bufflen); /* move to body processing now */
-
 
134
      }
-
 
135
 
-
 
136
    } else if (byteread < 0) { /* abort on error */
-
 
137
      return(-2); /* unexpected end of connection (while waiting for all http headers) */
-
 
138
 
-
 
139
    } else { /* else no data received - look for timeout and release a cpu cycle */
-
 
140
      if (time(NULL) - starttime > 20) return(-3); /* TIMEOUT! */
-
 
141
      _asm int 28h; /* release a CPU cycle */
-
 
142
    }
-
 
143
  }
-
 
144
}
-
 
145
 
-
 
146
 
110
/* fetch http data from ipaddr using url
147
/* fetch http data from ipaddr using url
111
 * write result to file outfname if not null, or print to stdout otherwise
148
 * write result to file outfname if not null, or print to stdout otherwise
112
 * fills bsum with the BSD sum of the data
149
 * fills bsum with the BSD sum of the data
113
 * returns the length of data obtained, or neg value on error */
150
 * returns the length of data obtained, or neg value on error */
114
static long htget(const char *ipaddr, const char *url, const char *outfname, unsigned short *bsum) {
151
static long htget(const char *ipaddr, const char *url, const char *outfname, unsigned short *bsum) {
115
  struct net_tcpsocket *sock;
152
  struct net_tcpsocket *sock;
116
  unsigned char buffer[4096];
153
  unsigned char buffer[4096];
117
  time_t lastactivity, lastprogressoutput = 0;
154
  time_t lastactivity, lastprogressoutput = 0;
118
  int headersdone = 0;
-
 
119
  int httpcode = -1;
155
  int httpcode = -1;
-
 
156
  int byteread;
120
  long flen = 0, lastflen = 0;
157
  long flen = 0, lastflen = 0;
121
  FILE *fd = NULL;
158
  FILE *fd = NULL;
122
 
159
 
123
  sock = net_connect(ipaddr, 80);
160
  sock = net_connect(ipaddr, 80);
124
  if (sock == NULL) {
161
  if (sock == NULL) {
Line 143... Line 180...
143
  if (net_send(sock, buffer, strlen((char *)buffer)) != (int)strlen((char *)buffer)) {
180
  if (net_send(sock, buffer, strlen((char *)buffer)) != (int)strlen((char *)buffer)) {
144
    puts("ERROR: failed to send HTTP query to remote server");
181
    puts("ERROR: failed to send HTTP query to remote server");
145
    goto SHITQUIT;
182
    goto SHITQUIT;
146
  }
183
  }
147
 
184
 
148
  lastactivity = time(NULL);
185
  /* receive and process HTTP headers */
149
  for (;;) {
-
 
150
    int byteread = net_recv(sock, buffer, sizeof(buffer) - 1); /* -1 because I will append a NULL terminator */
186
  byteread = htget_headers(buffer, sizeof(buffer), sock, &httpcode);
151
 
187
 
-
 
188
  /* transmission error? */
-
 
189
  if (byteread < 0) {
152
    if (byteread < 0) break; /* end of connection */
190
    printf("ERROR: communication error (%d)", byteread);
-
 
191
    puts("");
-
 
192
    goto SHITQUIT;
-
 
193
  }
153
 
194
 
154
    /*  */
-
 
155
    if (byteread == 0) {
-
 
156
      if (time(NULL) - lastactivity > 20) { /* TIMEOUT! */
195
  /* open destination file if required and if no server-side error occured */
157
        puts("ERROR: Timeout while waiting for data");
196
  if ((httpcode == 200) && (*outfname != 0)) {
158
        goto SHITQUIT;
197
    fd = fopen(outfname, "wb");
159
      }
198
    if (fd == NULL) {
160
      /* waiting for packets - release a CPU cycle in the meantime */
199
      printf("ERROR: failed to create file %s", outfname);
161
      _asm int 28h;
-
 
162
      /* */
200
      puts("");
163
      continue;
201
      goto SHITQUIT;
164
    }
202
    }
-
 
203
  }
-
 
204
 
-
 
205
  /* read body of the answer */
-
 
206
  lastactivity = time(NULL);
-
 
207
  for (;; byteread = net_recv(sock, buffer, sizeof(buffer) - 1)) { /* read 1 byte less because I need to append a NULL terminator */
165
 
208
 
166
    if (byteread > 0) {
209
    if (byteread > 0) { /* got data */
167
      buffer[byteread] = 0;
210
      buffer[byteread] = 0;
168
      lastactivity = time(NULL);
211
      lastactivity = time(NULL);
169
      /* are headers done already? */
-
 
170
      if (headersdone == 0) {
-
 
171
        if (httpcode < 0) { /* do I know the http code yet? */
-
 
172
          int spc;
-
 
173
          /* find the first space (HTTP/1.1 200 OK) */
-
 
174
          for (spc = 0; spc < 16; spc++) {
-
 
175
            if (buffer[spc] == ' ') break;
-
 
176
            if (buffer[spc] == 0) break;
-
 
177
          }
-
 
178
          if (buffer[spc] == 0) continue; /* not enough data received */
-
 
179
          if (buffer[spc] != ' ') {
-
 
180
            puts("ERROR: server answered with invalid HTTP");
-
 
181
            goto SHITQUIT;
-
 
182
          }
-
 
183
          httpcode = atoi((char *)(buffer + spc + 1));
-
 
184
          /* on error, the answer should be always printed on screen */
-
 
185
          if ((httpcode == 200) && (*outfname != 0)) {
-
 
186
            fd = fopen(outfname, "wb");
-
 
187
            if (fd == NULL) {
-
 
188
              printf("ERROR: failed to create file %s", outfname);
-
 
189
              puts("");
-
 
190
              goto SHITQUIT;
-
 
191
            }
-
 
192
          }
-
 
193
        }
-
 
194
        /* skip headers: look for \r\n\r\n or \n\n within the stream */
-
 
195
        byteread = detecthttpheadersend(buffer, byteread);
-
 
196
        headersdone = 1; /* assumes ALL headers are contained in the first packet TODO FIXME */
-
 
197
        if (byteread == 0) continue;
-
 
198
      }
-
 
199
      /* if downloading to file, write stuff to disk */
212
      /* if downloading to file, write stuff to disk */
200
      if (fd != NULL) {
213
      if (fd != NULL) {
201
        int i;
214
        int i;
202
        if (fwrite(buffer, 1, byteread, fd) != byteread) {
215
        if (fwrite(buffer, 1, byteread, fd) != byteread) {
203
          printf("ERROR: failed to write data to file %s after %ld bytes", outfname, flen);
216
          printf("ERROR: failed to write data to file %s after %ld bytes", outfname, flen);
Line 221... Line 234...
221
          *bsum += buffer[i];
234
          *bsum += buffer[i];
222
        }
235
        }
223
      } else { /* otherwise dump to screen */
236
      } else { /* otherwise dump to screen */
224
        printf("%s", buffer);
237
        printf("%s", buffer);
225
      }
238
      }
-
 
239
 
-
 
240
    } else if (byteread < 0) { /* end of connection */
-
 
241
      break;
-
 
242
 
-
 
243
    } else { /* check for timeout (byteread == 0) */
-
 
244
      if (time(NULL) - lastactivity > 20) { /* TIMEOUT! */
-
 
245
        puts("ERROR: Timeout while waiting for data");
-
 
246
        goto SHITQUIT;
-
 
247
      }
-
 
248
      /* waiting for packets - release a CPU cycle in the meantime */
-
 
249
      _asm int 28h;
226
    }
250
    }
227
  }
251
  }
228
  net_close(sock);
-
 
-
 
252
 
229
  return(flen);
253
  goto ALLGOOD;
230
 
254
 
231
  SHITQUIT:
255
  SHITQUIT:
-
 
256
  flen = -1;
-
 
257
 
-
 
258
  ALLGOOD:
-
 
259
  if (fd != NULL) fclose(fd);
232
  net_close(sock);
260
  net_close(sock);
233
  return(-1);
261
  return(flen);
234
}
262
}
235
 
263
 
236
 
264
 
237
/* checks if file exists, returns 0 if not, non-zero otherwise */
265
/* checks if file exists, returns 0 if not, non-zero otherwise */
238
static int fexists(const char *fname) {
266
static int fexists(const char *fname) {