Пример #1
0
void sendmesg(per_request* reqInfo, char *url, FILE *fp)
{
    char *loc, *furl;

    loc = newString(HUGE_STRING_LEN,STR_REQ);
    furl = newString(HUGE_STRING_LEN,STR_REQ);

    FClose(fp);
    if (!strstr(url, "://")) {   /*** If not a full URL ***/
      if (url[0] != '/') { /*** Relative URL ***/ 
        char *last = strrchr(reqInfo->url,'/');
        int x = 0, y = 0;
        while (((reqInfo->url+x) <= last) && (y < HUGE_STRING_LEN)) {
          loc[y] = *(reqInfo->url+x);
          x++; y++;
        }
        loc[y] = '\0';
	strncat(loc,url,HUGE_STRING_LEN - y);
      } else {
        strncpy(loc,url,HUGE_STRING_LEN);
      }
      construct_url(furl, reqInfo->hostInfo, loc);
      die(reqInfo,SC_REDIRECT_TEMP,furl);
    } else {
      die(reqInfo,SC_REDIRECT_TEMP,url);
    }
}
Пример #2
0
static void
xxx_to_url( fields *f, int n, char *http_prefix, char *urltag, newstr *xxx_url )
{
	newstr_empty( xxx_url );
	construct_url( http_prefix, fields_value( f, n, FIELDS_STRP ), xxx_url );
	if ( url_exists( f, urltag, xxx_url ) )
		newstr_empty( xxx_url );
}
Пример #3
0
Json::Value WykopSDK::construct_request(std::string rtype,
                        std::string rmethod,
                        std::vector<std::string> rmethod_params,
                        std::map<std::string, std::string> api_params,
                        std::map<std::string, std::string> post_params,
                        bool raw_response
){

    //rmethod_params = rmethod_params? rmethod_params:std::vector<std::string>();
    //api_params = api_params? api_params:std::map<std::string, std::string>();
    //post_params = post_params? post_params:std::map<std::string, std::string>();

    std::string url = construct_url(rtype, rmethod, rmethod_params, api_params);
    std::string apisign = get_request_sign(url, post_params);

    return request(url, post_params, apisign);
}
Пример #4
0
int send_vir_mode_page(av_req_data_t *data,char *buf,int len,request_t *req){
     int bytes;
     char *filename,*str;
     char *url;
     if(ci_simple_file_haseof(((av_req_data_t *)data)->body)){
	  if(data->error_page)
	       return ci_membuf_read(data->error_page,buf,len);
	  
	  
	  if(data->page_sent){
	       ci_debug_printf(10,"viralator:EOF received %d....\n",len);
	       return CI_EOF;
	  }

	  filename=((av_req_data_t *)data)->body->filename;
	  if((str=strrchr(filename,'/'))!=NULL)
	       filename=str+1;
	  
	  url=construct_url(VIR_HTTP_SERVER,data->requested_filename,req->user);

	  bytes=snprintf(buf,len,"Download your file(size=%"PRINTF_OFF_T") from <a href=\"%s%s\">%s</a> <br>",
			 ci_simple_file_size(((av_req_data_t *)data)->body),
			 url,
			 filename,
			 (data->requested_filename?data->requested_filename:((av_req_data_t *)data)->body->filename)
	       );
	  free(url);
	  data->page_sent=1;
	  return bytes;
     }
     
     
     
     if(  (((av_req_data_t *)data)->last_update+VIR_UPDATE_TIME) > time(NULL)){
	  return 0;
     }
     time(&(((av_req_data_t *)data)->last_update));
     ci_debug_printf(10,"Downloaded %"PRINTF_OFF_T" bytes from %"PRINTF_OFF_T" of data<br>",
	    ci_simple_file_size(((av_req_data_t *)data)->body), ((av_req_data_t *)data)->expected_size);
     return snprintf(buf,len,"Downloaded %"PRINTF_OFF_T" bytes from %"PRINTF_OFF_T" of data<br>",
		     ci_simple_file_size(((av_req_data_t *)data)->body), ((av_req_data_t *)data)->expected_size);
}
Пример #5
0
void 
http_announce(struct Torrent *t, int8_t event, CURL *connection, struct event_base *base)
{
     int32_t length = strlen(t->name) + strlen("/tmp/slug/-announce") + 1;
     char *filepath = malloc(length);
     char *url = construct_url(t, event);
     snprintf(filepath, length, "/tmp/slug/%s-announce", t->name);
     FILE *announce_data = fopen(filepath, "w");

     curl_easy_setopt(connection, CURLOPT_URL, url);
     curl_easy_setopt(connection, CURLOPT_WRITEDATA, announce_data);
     curl_easy_perform(connection);

     fclose(announce_data);
     announce_data = fopen(filepath, "r");

     /* read announce response into array */
     fseek(announce_data, 0, SEEK_END);
     int32_t pos = ftell(announce_data);
     fseek(announce_data, 0, SEEK_SET);
     char *data = malloc(pos);
     fread(data, pos, 1, announce_data);
     
     int64_t x = 0;
     struct BEncode *announceBEncode = parseBEncode(data, &x);

     assert(announceBEncode->type == BDict);
     if (failed(announceBEncode->cargo.bDict))
          pfailure_reason(announceBEncode->cargo.bDict);
     else {
          get_peers(t, data);
          t->announce_interval = get_interval(announceBEncode->cargo.bDict);
     }

     free(data);
     free(filepath);
     freeBEncode(announceBEncode);
}
Пример #6
0
BROKER_HANDLE Broker_Create(void)
{
    BROKER_HANDLE_DATA* result;

    /*Codes_SRS_BROKER_13_067: [Broker_Create shall malloc a new instance of BROKER_HANDLE_DATA and return NULL if it fails.]*/
    result = REFCOUNT_TYPE_CREATE(BROKER_HANDLE_DATA);
    if (result == NULL)
    {
        LogError("malloc returned NULL");
        /*return as is*/
    }
    else
    {
        /*Codes_SRS_BROKER_13_007: [Broker_Create shall initialize BROKER_HANDLE_DATA::modules with a valid VECTOR_HANDLE.]*/
        result->modules = singlylinkedlist_create();
        if (result->modules == NULL)
        {
            /*Codes_SRS_BROKER_13_003: [This function shall return NULL if an underlying API call to the platform causes an error.]*/
            LogError("VECTOR_create failed");
            free(result);
            result = NULL;
        }
        else
        {
            /*Codes_SRS_BROKER_13_023: [Broker_Create shall initialize BROKER_HANDLE_DATA::modules_lock with a valid LOCK_HANDLE.]*/
            result->modules_lock = Lock_Init();
            if (result->modules_lock == NULL)
            {
                /*Codes_SRS_BROKER_13_003: [This function shall return NULL if an underlying API call to the platform causes an error.]*/
                LogError("Lock_Init failed");
                singlylinkedlist_destroy(result->modules);
                free(result);
                result = NULL;
            }
            else
            {
                /*Codes_SRS_BROKER_17_001: [ Broker_Create shall initialize a socket for publishing messages. ]*/
                result->publish_socket = nn_socket(AF_SP, NN_PUB);
                if (result->publish_socket < 0)
                {
                    /*Codes_SRS_BROKER_13_003: [ This function shall return NULL if an underlying API call to the platform causes an error. ]*/
                    LogError("nanomsg puclish socket create failedL %d", result->publish_socket);
                    singlylinkedlist_destroy(result->modules);
                    Lock_Deinit(result->modules_lock);
                    free(result);
                    result = NULL;
                }
                else
                {
                    result->url = construct_url();
                    if (result->url == NULL)
                    {
                        /*Codes_SRS_BROKER_13_003: [ This function shall return NULL if an underlying API call to the platform causes an error. ]*/
                        singlylinkedlist_destroy(result->modules);
                        Lock_Deinit(result->modules_lock);
                        nn_close(result->publish_socket);
                        free(result);
                        LogError("Unable to generate unique url.");
                        result = NULL;
                    }
                    else
                    {
                        /*Codes_SRS_BROKER_17_004: [ Broker_Create shall bind the socket to the BROKER_HANDLE_DATA::url. ]*/
                        if (nn_bind(result->publish_socket, STRING_c_str(result->url)) < 0)
                        {
                            /*Codes_SRS_BROKER_13_003: [ This function shall return NULL if an underlying API call to the platform causes an error. ]*/
                            LogError("nanomsg bind failed");
                            singlylinkedlist_destroy(result->modules);
                            Lock_Deinit(result->modules_lock);
                            nn_close(result->publish_socket);
                            STRING_delete(result->url);                
                            free(result);
                            result = NULL;
                        }
                    }
                }
            }
        }
    }

    /*Codes_SRS_BROKER_13_001: [This API shall yield a BROKER_HANDLE representing the newly created message broker. This handle value shall not be equal to NULL when the API call is successful.]*/
    return result;
}
Пример #7
0
int
print_tldrpage(char const* input)
{
    int islinux;
    int isdarwin;
    char* output;
    char* platform;
    char url[1024];
    struct utsname sys;

    int homelen;
    char* homedir;
    int tldrlen;
    char* tldrdir = "/.tldr/tldr-master/pages/";
    char directory[1024];
    struct stat sb;

    uname(&sys);
    islinux = strcmp(sys.sysname, "Linux") == 0;
    isdarwin = strcmp(sys.sysname, "Darwin") == 0;

    if (islinux) { platform = "linux"; }
    else if (isdarwin) { platform = "osx"; }
    else { platform = "common"; }

    if ((homedir = getenv("HOME")) == NULL)
    {
        homedir = getpwuid(getuid())->pw_dir;
    }

    homelen = strlen(homedir);
    tldrlen = strlen(tldrdir);
    memcpy(directory, homedir, homelen);
    memcpy(directory + homelen, tldrdir, tldrlen);
    directory[homelen + tldrlen] = '\0';

    if (stat(directory, &sb) == 0 && S_ISDIR(sb.st_mode))
    {
        construct_path(url, 1024, homedir, input, platform);
        if (stat(url, &sb) == 0 && S_ISREG(sb.st_mode))
        {
            if (!get_file_content(url, &output, 0))
            {
                parse_tldrpage(output);
                free(output);
                return 0;
            }
        }
        else
        {
            construct_path(url, 1024, homedir, input, "common");
            if (stat(url, &sb) == 0 && S_ISREG(sb.st_mode))
            {
                if (!get_file_content(url, &output, 0))
                {
                    parse_tldrpage(output);
                    free(output);
                    return 0;
                }
            }
        }
    }

    construct_url(url, 1024, input, platform);
    download_content(url, &output, 0);
    if (output == NULL)
    {
        construct_url(url, 1024, input, "common");
        download_content(url, &output, 0);
        if (output == NULL) { return 1; }
    }

    parse_tldrpage(output);

    free(output);
    return 0;
}
Пример #8
0
void send_file(per_request *reqInfo, struct stat *fi, char allow_options) 
{
    FILE *f;
#ifdef BLACKOUT_CODE
    int isblack = FALSE;
#endif /* BLACKOUT_CODE */    

    if ((reqInfo->method != M_GET) && (reqInfo->method != M_HEAD)) {
	sprintf(error_msg,"%s to non-script",methods[reqInfo->method]);
	die(reqInfo,SC_NOT_IMPLEMENTED,error_msg);
    }
    set_content_type(reqInfo,reqInfo->filename);

    if((allow_options & OPT_INCLUDES) && (!reqInfo->outh_content_encoding[0])) {
#ifdef XBITHACK
        if((fi->st_mode & S_IXUSR) ||
           (!strcmp(reqInfo->outh_content_type,INCLUDES_MAGIC_TYPE))) {
#else
	if(!strcmp(reqInfo->outh_content_type,INCLUDES_MAGIC_TYPE)) {
#endif /* XBITHACK */
	    reqInfo->bytes_sent = 0;
	    send_parsed_file(reqInfo, allow_options & OPT_INCNOEXEC);
	    log_transaction(reqInfo);
	    return;
	}
    }
    if (reqInfo->path_info[0]) {
	strcat(reqInfo->filename,reqInfo->path_info);
	strcat(reqInfo->url,reqInfo->path_info);
	sprintf(error_msg,"No file matching URL: %s",reqInfo->url);
	log_reason(reqInfo, error_msg, reqInfo->filename);
	die(reqInfo,SC_NOT_FOUND,reqInfo->url);
    }
	
    if(!(f=FOpen(reqInfo->filename,"r"))) {
      if (errno == EACCES) {
	log_reason(reqInfo,"(1) file permissions deny server access",
		   reqInfo->filename);
	/* we've already established that it exists */
	die(reqInfo,SC_FORBIDDEN,reqInfo->url); 
      } else {
	/* We know an error occured, of an unexpected variety. 
	 * This could be due to no more file descriptors.  We have this
	 * child exit after this stage so that errors of state are 
	 * swept under the carpet.
	 */
	standalone = 0;
	sprintf(error_msg,"File Open error, errno=%d",errno);
	log_reason(reqInfo,error_msg,reqInfo->filename);
	die(reqInfo,SC_SERVER_ERROR,error_msg);
      }
    }
    reqInfo->bytes_sent = 0;

#ifdef BLACKOUT_CODE
    if (!strcmp(reqInfo->outh_content_type,BLACKOUT_MAGIC_TYPE)) {
      isblack = TRUE;
      strcpy(reqInfo->outh_content_type,"text/html");
    }
#endif /* BLACKOUT_CODE */

    if(reqInfo->http_version != P_HTTP_0_9) {
      /* No length dependent headers since black is parsed */
#ifdef BLACKOUT_CODE
      if (isblack == FALSE) { 
#endif /* BLACKOUT_CODE */
#ifdef CONTENT_MD5
	reqInfo->outh_content_md5 = (unsigned char *)md5digest(f);
#endif /* CONTENT_MD5 */
	set_content_length(reqInfo,fi->st_size);
	if (set_last_modified(reqInfo,fi->st_mtime)) {
	    FClose(f);
	    return;
	}
      }
      if (reqInfo->http_version != P_HTTP_0_9) {
           send_http_header(reqInfo);
      }
#ifdef BLACKOUT_CODE
    }
#endif /* BLACKOUT_CODE */

    if(reqInfo->method != M_HEAD) {
#ifdef BLACKOUT_CODE
      if (isblack == TRUE)
	send_fp_black(reqInfo,f,NULL);
       else
#endif /* BLACKOUT_CODE */
	send_fp(reqInfo,f,NULL);
    }
    log_transaction(reqInfo);
    FClose(f);
}


void send_dir(per_request *reqInfo,struct stat *finfo, char allow_options) {
  char *name_ptr, *end_ptr;
  char *ifile, *temp_name;

  ifile = newString(HUGE_STRING_LEN,STR_TMP);
  temp_name = newString(HUGE_STRING_LEN,STR_TMP);

/* Path Alias (pa) array should now have the trailing slash */
  /*  if (pa[0] != '/') { */
  if ((reqInfo->filename[strlen(reqInfo->filename) - 1] != '/') && 
      (reqInfo->path_info[0] != '/')) {
    strcpy_dir(ifile,reqInfo->url);
    construct_url(temp_name,reqInfo->hostInfo,ifile);
    escape_url(temp_name);
    die(reqInfo,SC_REDIRECT_PERM,temp_name);
  }

  /* Don't allow PATH_INFO to directory indexes as a compromise for 
     error messages for files which don't exist */

  if ((reqInfo->path_info[0] != '\0') || (strlen(reqInfo->path_info) > 1)) {
        strcat(reqInfo->filename,reqInfo->path_info);
        strcat(reqInfo->url,reqInfo->path_info);
        sprintf(error_msg,"No file matching URL: %s",reqInfo->url);
        log_reason(reqInfo, error_msg, reqInfo->filename);
	freeString(temp_name);
	freeString(ifile);
        die(reqInfo,SC_NOT_FOUND,reqInfo->url);
  }
    
  strncpy(temp_name, reqInfo->hostInfo->index_names, HUGE_STRING_LEN-1);
  end_ptr = name_ptr = temp_name;

  while (*name_ptr) {
    
    while (*name_ptr && isspace (*name_ptr)) ++name_ptr;
    end_ptr = name_ptr;
    if (strchr(end_ptr, ' ') ) {
      end_ptr = strchr(name_ptr, ' ');
      *end_ptr = '\0';
      end_ptr++;
    } else
      end_ptr += strlen(end_ptr);
    make_full_path(reqInfo->filename,name_ptr,ifile);
    if(stat(ifile,finfo) == -1) {
      if(! *end_ptr && (allow_options & OPT_INDEXES)) {
        if (reqInfo->path_info[0]) {
          strcat(reqInfo->filename,reqInfo->path_info);
	  strcat(reqInfo->url,reqInfo->path_info);
	  log_reason(reqInfo,"file does not exist",reqInfo->filename);
	  freeString(ifile);
	  freeString(temp_name);
	  die(reqInfo,SC_NOT_FOUND,reqInfo->url);
	}
	if ((reqInfo->method != M_GET) && (reqInfo->method != M_HEAD)) {
	  sprintf(error_msg,"%s to non-script",methods[reqInfo->method]);
	  freeString(ifile);
	  freeString(temp_name);
	  die(reqInfo,SC_NOT_IMPLEMENTED,error_msg);
	}	
	index_directory(reqInfo);
	freeString(ifile);
	freeString(temp_name);
	return;
      } else if (! *end_ptr) {
	log_reason(reqInfo,"(2) file permissions deny server access",
		   reqInfo->filename);
	freeString(ifile);
	freeString(temp_name);
	die(reqInfo,SC_FORBIDDEN,reqInfo->url);
      }
    } else {
      strcpy(reqInfo->filename,ifile);
      probe_content_type(reqInfo,reqInfo->filename);
      if(!strcmp(reqInfo->outh_content_type,CGI_MAGIC_TYPE))
	send_cgi(reqInfo,finfo,allow_options);
      else
	send_file(reqInfo,finfo,allow_options);
      freeString(ifile);
      freeString(temp_name);
      return;
    }
    name_ptr = end_ptr;
  }	 
}