Пример #1
0
int fetch(unsigned char *sha1)
{
	if (has_sha1_file(sha1))
		return 0;
	else
		return fetch_file(sha1) && fetch_pack(sha1);
}
Пример #2
0
  bool
  downloader::fetch(url_t const& _url, std::ostream& out_stream) const
  {
    string_t url(get_full_url(_url));

    download_t *download = new download_t(url);
    download->stream = &out_stream;

    return fetch_file(url, download, true);
  }
Пример #3
0
  bool
  downloader::fetch(url_t const& _url, string_t& out_buf) const
  {
    string_t url(get_full_url(_url));

    download_t *download = new download_t(url);
    download->buf = &out_buf;

    return fetch_file(url, download, true);
  }
Пример #4
0
/*
 * execute_file executes a plain text script that has already been assigned
 * as the input file.
 *
 * The arguments must have been assigned, too.
 *
 * The return value is the value that the main routine should return to the OS.
 */
static int execute_file( tsd_t *TSD )
{
   FILE *fptr = TSD->systeminfo->input_fp;
   internal_parser_type parsing;
   streng *string;
   int RetCode;

   /*
    * From here we are interpreting...
    */
   fetch_file( TSD, fptr ? fptr : stdin, &parsing );
   if ( fptr )
      fclose( fptr );
   TSD->systeminfo->input_fp = NULL;

   if ( parsing.result != 0 )
      exiterror( ERR_YACC_SYNTAX, 1, parsing.tline );
   else
      TSD->systeminfo->tree = parsing;

#if !defined(MINIMAL) && !defined(VMS) && !defined(DOS) && !defined(_MSC_VER) && !defined(__IBMC__) && !defined(MAC)
   if ( !fptr )
   {
      struct stat buffer;
      int rc;

      rc = fstat( fileno( stdin ), &buffer );
      if ( ( rc == 0 ) && S_ISCHR( buffer.st_mode ) )
      {
         /*
          * FIXME. MH and FGC.
          * When does this happen. Add debugging code to determine this, because
          * after 2 glasses of rocket fuel it seems silly to have this code!
          * 13-5-2004.
          */
         printf( "  \b\b" );
         fflush( stdout );
         rewind( stdin );
      }
   }
#endif

   flush_trace_chars( TSD );

   string = interpret( TSD, TSD->systeminfo->tree.root );
   RetCode = codeFromString( TSD, string );
   if ( string )
      Free_stringTSD( string );

   return RetCode;
}
Пример #5
0
void accept_request(int clientfd){
	char path[255];
	char line[1024];
	char method[255];
	char url[255];
	int numread = 0;
	numread = get_line(clientfd, line, sizeof(line));
	char *pos = getToken(line, method, sizeof(method));
	pos = getToken(pos, url, sizeof(url));

	//Method not implemented.
	if(strcasecmp(method, "POST") != 0 && strcasecmp(method, "GET") != 0){
		strcpy(url, "/501.html");
	}
	if(strcasecmp(method, "GET") == 0){

	}
	if(strcmp(url,"/") == 0){
		strcpy(url+1, "index.html");
	}
	sprintf(path, "%s%s", FOLDER, url);
	fetch_file(clientfd, path);
}
Пример #6
0
int handle_request(int connfd) {
    char *buf = (char *) malloc(MAXLINE);
    char *method, *uri, *version;
    char *fullpath;
    int nread;
    struct stat fs;
    int is_dir;
    int error_code = 0, ret = 0;
    rio_t rio;

    rio_initb(&rio, connfd);

    nread = rio_readlineb(&rio, buf, MAXLINE - 1);
    if (nread == 0) {
        error_code = 400;
        goto fail;
    } else if (buf[nread - 1] != '\n') {
        error_code = 414;
        goto fail;
    } else {
        buf[nread - 1] = 0;
        if (nread >= 2 && buf[nread - 2] == '\r') {
            buf[nread - 2] = 0;
        }
    }
    method = buf;
    for (uri = method; *uri != 0; ++uri) {
        if (*uri == ' ') {
            *uri = 0;
            ++uri;
            break;
        }
    }
    for (version = uri; *version != 0; ++version) {
        if (*version == ' ') {
            *version = 0;
            ++version;
            break;
        }
    }
    if (*method == 0 || *uri == 0 || *version == 0) {
        /* missing some fields in http request header */
        error_code = 400;
        goto fail;
    }
    if (strcmp(method, "GET") != 0) {
        /* only allow GET method */
        error_code = 405;
        goto fail;
    }

    goto next;

fail:
    free(buf);
    if (error_code > 0) {
        send_error(connfd, error_code);
        return 0;
    }

next:
    /* translate path and dispatch HTTP request according to path */
    uri = translate_path(uri);
    fullpath = (char *) malloc(strlen(uri) + 2);
    fullpath[0] = '.';
    strcpy(fullpath + 1, uri);

    if (stat(fullpath, &fs) != 0) {
        send_error(connfd, 404);
    } else {
        is_dir = S_ISDIR(fs.st_mode);
        if (is_dir) {
            if (fullpath[(int) strlen(fullpath) - 1] != '/') {
                send_response(connfd, 301, NULL);
                send_header(connfd, "Location", "%s/", fullpath);
                end_headers(connfd);
            } else {
                ret = list_directory(connfd, fullpath);
            }
        } else {
            ret = fetch_file(connfd, fullpath);
        }
    }
    free(fullpath);
    free(buf);

    return ret;
}
Пример #7
0
int list_directory(int connfd, const char *dirname) {
    char *buf;
    DIR *d;
    struct dirent *dir;
    struct stat fs;
    const char *item_fmt;
    const char *filename;
    char *fullname;
    int len1, len2, nwritten = 0;
    int ret;

    d = opendir(dirname);
    if (d) {

        buf = (char *) malloc(MAXBUF);

        /* TODO: escape or quote dirname / filename */
        nwritten += snprintf(buf + nwritten, MAXBUF - nwritten,
                             "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 3.2 Final//EN\">\n"
                             "<html>\n"
                             "<title>Directory listing for %s</title>\n"
                             "<body>\n"
                             "<h2>Directory listing for %s</h2>\n"
                             "<hr>\n"
                             "<ul>\n", dirname + 1, dirname + 1);

        while ((dir = readdir(d)) != NULL) {
            filename = dir->d_name;

            if (strcmp(filename, ".") == 0 || strcmp(filename, "..") == 0) {
                continue;
            } else {
                len1 = strlen(dirname);
                len2 = strlen(filename);
                fullname = (char *) malloc(len1 + len2 + 1);
                strcpy(fullname, dirname);
                strcpy(fullname + len1, filename);
                fullname[len1 + len2] = 0;

                if (strcasecmp(filename, "index.html") == 0 || strcasecmp(filename, "index.htm") == 0) {
                    ret = fetch_file(connfd, fullname);
                    free(fullname);
                    free(buf);
                    closedir(d);
                    return ret;
                } else {
                    stat(fullname, &fs);
                    if (S_ISDIR(fs.st_mode)) {
                        item_fmt = "<li><a href=\"%s/\">%s/</a>\n";
                    } else {
                        item_fmt = "<li><a href=\"%s\">%s</a>\n";
                    }
                    nwritten += snprintf(buf + nwritten, MAXBUF - nwritten,
                                         item_fmt, filename, filename);
                }

                free(fullname);
            }
        }
        closedir(d);

        nwritten += snprintf(buf + nwritten, MAXBUF - nwritten,
                             "</ul>\n"
                             "<hr>\n"
                             "</body>\n"
                             "</html>\n");

        send_response(connfd, 200, NULL);
        send_header(connfd, "Content-Type", "text/html");
        send_header(connfd, "Content-Length", "%d", nwritten);
        end_headers(connfd);
        rio_writen(connfd, buf, nwritten);

        free(buf);

        return 1;
    }

    send_error(connfd, 404);

    return 0;
}
Пример #8
0
int main(int argc, char **argv)
{
   char config_file_path[256];
   opt_filename = NULL;
   opt_url = NULL;
   fetch_all = false;
   test_mode = false;
   verbose = false;
   opt_insecure = false;
   used_insecure = false;

   strcpy(config_file_path, "/etc/openrail.conf");
   word usage = false;
   int c;
   while ((c = getopt (argc, argv, ":c:u:f:tpih")) != -1)
      switch (c)
      {
      case 'c':
         strcpy(config_file_path, optarg);
         break;
      case 'u':
         if(!opt_filename) opt_url = optarg;
         break;
      case 'f':
         if(!opt_url) opt_filename = optarg;
         break;
      case 'a':
         fetch_all = true;
         break;
      case 't':
         test_mode = true;
         break;
      case 'p':
         verbose = true;
         break;
      case 'i':
         opt_insecure = true;
         break;
      case 'h':
         usage = true;
         break;
      case ':':
         break;
      case '?':
      default:
         usage = true;
      break;
      }

   char * config_fail;
   if((config_fail = load_config(config_file_path)))
   {
      printf("Failed to read config file \"%s\":  %s\n", config_file_path, config_fail);
      usage = true;
   }

   if(usage) 
   {
      printf("%s %s  Usage: %s [-c /path/to/config/file.conf] [-u <url> | -f <path> | -a] [-t | -r] [-p][-i]\n", NAME, BUILD, argv[0]);
      printf(
             "-c <file>  Path to config file.\n"
             "Data source:\n"
             "default    Fetch latest update.\n"
             "-u <url>   Fetch from specified URL.\n"
             "-f <file>  Use specified file.  (Must already be decompressed.)\n"
             "Actions:\n"
             "default    Apply data to database.\n"
             "-t         Report datestamp on download or file, do not apply to database.\n"
             "Options:\n"
             "-i         Insecure.  Circumvent certificate checks if necessary.\n"
             "-p         Print activity as well as logging.\n"
             );
      exit(1);
   }

   char zs[1024];

   start_time = time(NULL);

   debug = *conf[conf_debug];
  
   _log_init(debug?"/tmp/tscdb.log":"/var/log/garner/tscdb.log", (debug?1:(verbose?4:0)));
   
   _log(GENERAL, "");
   _log(GENERAL, "%s %s", NAME, BUILD);
   
   // Enable core dumps
   struct rlimit limit;
   if(!getrlimit(RLIMIT_CORE, &limit))
   {
      limit.rlim_cur = RLIM_INFINITY;
      setrlimit(RLIMIT_CORE, &limit);
   }
   
   int i;
   for(i = 0; i < MATCHES; i++)
   {
      if(regcomp(&match[i], match_strings[i], REG_ICASE + REG_EXTENDED))
      {
         sprintf(zs, "Failed to compile regex match %d", i);
         _log(MAJOR, zs);
      }
   }
   
   // Initialise database
   if(db_init(conf[conf_db_server], conf[conf_db_user], conf[conf_db_password], conf[conf_db_name])) exit(1);

   {
      word e;
      if((e=database_upgrade(cifdb)))
      {
         _log(CRITICAL, "Error %d in upgrade_database().  Aborting.", e);
         exit(1);
      }
   }

   run = 1;
   tiploc_ignored = false;

   // Zero the stats
   {
      word i;
      for(i = 0; i < MAXStats; i++) { stats[i] = 0; }
   }

   if(fetch_file())
   {
      if(opt_url || opt_filename)
      {
         _log(GENERAL, "Failed to find data.");
         exit(1);
      }
      {
         char report[256];
         _log(GENERAL, "Failed to fetch file.");
         
         sprintf(report, "Failed to collect timetable update after %lld attempts.", stats[Fetches]);
         email_alert(NAME, BUILD, "Timetable Update Failure Report", report);
      }
      exit(1);
   }

   char in_q = 0;
   char b_depth = 0;

   size_t ibuf = 0;
   size_t iobj = 0;
   size_t buf_end;

   // Determine applicable update
   {
      MYSQL_RES * result0;
      MYSQL_ROW row0;
      last_update_id[0] = '\0';
      if(!db_query("SELECT MAX(id) FROM updates_processed"))
      {
         result0 = db_store_result();
         if((row0 = mysql_fetch_row(result0)))
         {
            strcpy(last_update_id, row0[0]);
         }
         mysql_free_result(result0);
      }
   }

   if(last_update_id[0] == '\0')
   {
      _log(CRITICAL, "Failed to determine last update id from database.");
      exit(1);
   }


   if(test_mode)
   {
   }
   else
   {
      char c, pc;
      pc = 0;
      // Run through the file splitting off each JSON object and passing it on for processing.

      // DB may have dropped out due to long delay
      (void) db_connect();
      if(db_start_transaction()) _log(CRITICAL, "Failed to initiate database transaction.");
      while((buf_end = fread(buffer, 1, MAX_BUF, fp_result)) && run && !db_errored)
      {
         for(ibuf = 0; ibuf < buf_end && run && !db_errored; ibuf++)
         {
            c = buffer[ibuf];
            if(c != '\r' && c != '\n') 
            {
               obj[iobj++] = c;
               if(iobj >= MAX_OBJ)
               {
                  _log(CRITICAL, "Object buffer overflow!");
                  exit(1);
               }
               if(c == '"' && pc != '\\') in_q = ! in_q;
               if(!in_q && c == '{') b_depth++;
               if(!in_q && c == '}' && b_depth-- && !b_depth)
               {
                  obj[iobj] = '\0';
                  process_object(obj);
                  iobj = 0;
               }
            }
            pc = c;
         }
      }
      fclose(fp_result);
      if(db_errored)
      {
         _log(CRITICAL, "Update rolled back due to database error.");
         (void) db_rollback_transaction();
      }
      else
      {
         _log(GENERAL, "Committing database updates...");
         if(db_commit_transaction())
         {
            _log(CRITICAL, "Database commit failed.");
         }
         else
         {
            _log(GENERAL, "Committed.");
         }
      }
   }

#define REPORT_SIZE 16384
   char report[REPORT_SIZE];
   report[0] = '\0';

   _log(GENERAL, "");
   _log(GENERAL, "End of run:");

   if(used_insecure)
   {
      strcat(report, "*** Warning: Insecure download used.\n");
      _log(GENERAL, "*** Warning: Insecure download used.");
   }

   sprintf(zs, "             Elapsed time: %ld minutes", (time(NULL) - start_time + 30) / 60);
   _log(GENERAL, zs);
   strcat(report, zs); strcat(report, "\n");
   if(test_mode)
   {
      sprintf(zs, "Test mode.  No database changes made.");
      _log(GENERAL, zs);
      strcat(report, zs); strcat(report, "\n");
      exit(0);
   }

   for(i=0; i<MAXStats; i++)
   {
      sprintf(zs, "%25s: %s", stats_category[i], commas_q(stats[i]));
      if(i == DBError && stats[i]) strcat(zs, " ****************");
      _log(GENERAL, zs);
      strcat(report, zs);
      strcat(report, "\n");
   }

   db_disconnect();

   email_alert(NAME, BUILD, "Timetable Update Report", report);

   exit(0);
}