Example #1
0
SPObject* sp_css_uri_reference_resolve( SPDocument *document, const gchar *uri )
{
    SPObject* ref = NULL;

    if ( document && uri && ( strncmp(uri, "url(", 4) == 0 ) ) {
        gchar *trimmed = extract_uri( uri );
        if ( trimmed ) {
            ref = sp_uri_reference_resolve( document, trimmed );
            g_free( trimmed );
        }
    }

    return ref;
}
Example #2
0
static int
parse_icap_startline(char *line,struct pollfd_state *pfd,icap_status *istat){
	icap_state *is = get_pfd_icap(pfd);
	const char *method;

	is->method = parse_method(&line);
	if(is->method == ICAP_METHOD_COUNT){
		*istat = ICAPSTATUS_METHOD_NOT_IMPLEMENTED;
		return -1;
	}
	if((method = name_icap_method(is->method)) == NULL){
		bitch("Couldn't stringize method %d\n",is->method);
		*istat = ICAPSTATUS_INTERNAL_ERROR;
		return -1;
	}
	// nag("Valid method: %s\n",method);
	inc_method_begun(is->method);
	if((is->icapuri = extract_uri(ICAP_URI_SCHEME,&line)) == NULL){
		*istat = ICAPSTATUS_BAD_REQUEST;
		return -1;
	}
	if(verify_icap_uri(is->icapuri,is)){
		bitch("Bad URI for %s\n",method);
		free_uri(&is->icapuri);
		*istat = ICAPSTATUS_METHOD_BAD_SERVICE;
		return -1;
	}
	if(parse_icap_version(&line)){
		*istat = ICAPSTATUS_VERSION_NOT_SUPPORTED;
		return -1;
	}
	if(strcmp(line,CRLF)){
		bitch("Excess data after ICAP version (%zu/%zu)\n",
				strlen(line),__builtin_strlen(CRLF));
		*istat = ICAPSTATUS_BAD_REQUEST;
		return -1;
	}
	return 0;
}
Example #3
0
int modify_url_cookie_for_safe_search(char **url, const char *cookie, char **new_cookie,
				      const SiteInfoType *si) {
  uri *u;
  char *urls, *urls2;
  int is_google = 0, is_yahoo = 0, is_msn = 0, is_alltheweb = 0, is_altavista = 0, is_a9 = 0, is_bing = 0, is_known = 0;
  int url_modified = 0;
  ustring us = USTRING_INITIALIZER;

  if(cookie) {
    if(!new_cookie) {
      bitch("Passed cookie but no pointer for rewritten cookie\n");
      return 0;
    }
    *new_cookie = 0;
  }

  urls = urls2 = Strdup(*url);
  u = extract_uri(0, &urls);
  Free(urls2);
  if(!u) {
    nag("URI extraction failed\n");
    return 0;
  }
  
  if(u->host) {
    if(strcasestr(u->host, "google.") && !strcasestr(u->host, "maps.google.")) {
      is_google = is_known = 1;
    } else if(strcasestr(u->host, "yahoo.")) {
      is_yahoo = is_known = 1;
    } else if(strcasestr(u->host, ".msn.") || strcasestr(u->host, "search.live.")) {
      is_msn = is_known = 1;
    } else if(strcasestr(u->host, "alltheweb.")) {
      is_alltheweb = is_known = 1;
    } else if(strcasestr(u->host, "altavista.")) {
      is_altavista = is_known = 1;
    } else if(strcasestr(u->host, "a9.")) {
      is_a9 = is_known = 1;
    } else if(strcasestr(u->host, ".bing.com")) {
      is_bing = is_known = 1;
    }

    // xxx WW uses some additional heuristics here based on category information and the presence of certain strings in the cookie

    if(!is_known) {
      size_t i;
      int is_ip = 1;
      for(i = 0; i < strlen(u->host); i++) {
	if(isalpha(((const unsigned char *)u->host)[i])) {
	  is_ip = 0;
	  break;
	}
      }
      if(is_ip && u->query && is_search_engine(si)) {
	if(strncmp(u->query, "q=", 2) == 0 || strstr(u->query, "&q=")) {
	  is_google = is_known = 1;
	} else if(strncmp(u->query, "p=", 2) == 0 || strstr(u->query, "&p=")) {
	  is_yahoo = is_known = 1;
	}
      }
    }
  }

  if(is_google && u->query) {
    url_modified = process_google_query(&(u->query));
  } else if(is_yahoo && u->query) {
    url_modified = process_yahoo_query(&(u->query), u->path);
  } else if(is_msn && cookie) {
    process_msn_cookie(cookie, new_cookie);
  } else if(is_alltheweb && cookie) {
    process_alltheweb_cookie(cookie, new_cookie);
    if(new_cookie) {
      url_modified = process_alltheweb_query(&(u->query));
    }
  } else if(is_altavista && cookie) {
    process_altavista_cookie(cookie, new_cookie);
    if(new_cookie) {
      url_modified = process_altavista_query(&(u->query));
    }
  } else if(is_bing && u->query) {
    url_modified = process_bing_query(&(u->query));
  }

  if(url_modified) {
    char *newurl;
    if(stringize_uri(&us,u) < 0) {
      nag("Error stringizing uri\n");
      free_uri(&u);
      return 0;
    }
    newurl = Strdup(us.string);
    reset_ustring(&us);
    if(!newurl) {
      free_uri(&u);
      return 0;
    }
    Free(*url);
    *url = newurl;
    nag("modified URL\n");
  }
  
  free_uri(&u);
  return url_modified;
}
Example #4
0
int rtp_worker_fun() {
    unsigned short rtp_port;
    unsigned short rtcp_port;
    struct msg_to_worker message;
    struct msgbuf die_message;
    int st;
    RTP_TO_RTSP response;
    char *abs_path = 0, *host = 0, *path = 0, *end_filename;

    /* Signal handler para el worker */
    signal(SIGINT, rtp_worker_stop);

    /* Set paused time */
    gettimeofday(&time_pause, 0);

    /* Abrir cola de mensajes */
    msg_queue = msgget(MSG_IDENTIFIER/*TODO: Don't hardcode this */, IPC_CREAT /*| IPC_EXCL */| 0700);
    if (msg_queue == -1) goto terminate_error;

    message.mtype = getpid();
    /* Wait for SETUP message */
    st = msgrcv(msg_queue, &message, sizeof(struct msg_to_worker) - sizeof(long), getpid(), 0);
    if (st == -1) goto terminate_error;


    /* Change the response port to the one indicated in the message */
    ((struct sockaddr_in*)&(message.rtsp_socket))->sin_port = message.message.response_port;
    /* Open response socket to RTSP server */
    rtsp_sockfd = socket(PF_INET, SOCK_STREAM, 0);
    if (rtsp_sockfd == -1) goto terminate_error;

    st = connect(rtsp_sockfd, (struct sockaddr *)&(message.rtsp_socket), sizeof(struct sockaddr));
    if (st == -1) goto terminate_error;

    /* Bind two consecutive UDP ports */
    rtp_port =  bind_UDP_ports(&rtp_sockfd, &rtcp_sockfd);
    if (!rtp_port) goto terminate_error;
    rtcp_port = rtp_port + 1;

    /* NOTE: This isn't really necesary for the server to work */
    /* Create socket for rtsp */
    /*    rtsp_sockfd = socket(PF_INET, SOCK_STREAM, 0);
        if (!rtsp_sockfd) goto terminate_error;
        ((struct sockaddr_in *)&message.rtsp_socket)->sin_port = comm_port;
        st = connect(rtsp_sockfd, (struct sockaddr *)&message.rtsp_socket, sizeof(message.rtsp_socket));
        if (st == -1) goto terminate_error; */

    /* TODO: Set media type */
    if (strstr(message.message.uri, "audio"))
        media_type = AUDIO;
    else
        media_type = VIDEO;
    client_ip = message.message.client_ip;
    client_port = message.message.client_port;

    /* Get the absolute path to the file */
    st = extract_uri(message.message.uri, &host, &path);
    free(host);
    host = 0;
    if (!st) goto terminate_error;
    abs_path = get_absolute_path(path);
    free(path);
    path = 0;
    if (!abs_path) goto terminate_error;
    end_filename = strstr(abs_path, "/audio");
    if (!end_filename)
        end_filename = strstr(abs_path, "/video");
    *end_filename = 0;

    /* Initialize gstreamer */
    st = gstreamer_fun(abs_path);
    free(abs_path);
    abs_path = 0;
    if (!st) goto terminate_error;
    /* Set as paused */
    pthread_mutex_lock(&play_state_mutex);

    /* Initialize the pipe for communication with the sleeper process */
    st = pipe(sleeper_pipe);
    if (st == -1) goto terminate_error;
    /* Initialize the process that will make this process sleep to limit the rate */
    sleeper_pid = fork();
    if (sleeper_pid < 0) goto terminate_error;
    else if (sleeper_pid == 0) sleeper_fun();

    /* Initialize gstreamer communication threads, that will send data to the client */
    st = pthread_create(&gstreamer_comm_thread, 0, gstreamer_comm_thread_fun, &message.message.ssrc);
    if (st) goto terminate_error;
    gstreamer_comm_created = 1;

    /* Initialize the thread responsible for limiting the speed of gstreamer */
    st = pthread_create(&gstreamer_limitrate_thread, 0, gstreamer_limitrate_thread_fun, 0);
    if (st) goto terminate_error;
    gstreamer_limitrate_created = 1;

    /* Initialize the thread that will run gstreamer */
    st = pthread_create(&gstreamer_loop_thread, 0, gstreamer_loop_thread_fun, 0);
    if (st) goto terminate_error;
    gstreamer_loop_created = 1;
    /* TODO: Create rtcp thread */

    /* Prepare response */
    response.Session = message.message.Session;
    strcpy(response.uri, message.message.uri);
    response.ssrc = message.message.ssrc;
    response.order = OK_RTP;
    response.server_port = rtp_port;
    st = send(rtsp_sockfd, &response, sizeof(RTP_TO_RTSP), 0);

    close(rtsp_sockfd);
    rtsp_sockfd = -1;

    for (;;) {
        /* Wait for message */
        st = msgrcv(msg_queue, &message, sizeof(struct msg_to_worker) - sizeof(long), getpid(), 0);
        if (st == -1) goto terminate_error;

        /* Change the response port to the one indicated in the message */
        ((struct sockaddr_in*)&(message.rtsp_socket))->sin_port = message.message.response_port;
        /* Open response socket to RTSP server */
        rtsp_sockfd = socket(PF_INET, SOCK_STREAM, 0);
        if (rtsp_sockfd == -1) goto terminate_error;

        st = connect(rtsp_sockfd, (struct sockaddr *)&message.rtsp_socket, sizeof(struct sockaddr));
        if (st == -1) goto terminate_error;

        switch (message.message.order) {
        case PLAY_RTP: {
            GstStateChangeReturn st_ret;
            fprintf(stderr, "Recibido play en proceso %d\n", getpid());
            /* TODO: Send play command to gstreamer thread */
            /* TODO: goto error if error */
            do {

                GstState state;
                GstState pending;

                fprintf(stderr, "Trying play in process %d\n", getpid());
                gst_element_set_state(pipeline, GST_STATE_PLAYING);

                st_ret = gst_element_get_state(pipeline, &state, &pending, GST_CLOCK_TIME_NONE);
                if (st_ret == GST_STATE_CHANGE_SUCCESS)
                    fprintf(stderr, "Successful play\n");
                else if (st_ret == GST_STATE_CHANGE_FAILURE)
                    fprintf(stderr, "Error in play\n");
            } while (st_ret == GST_STATE_CHANGE_ASYNC || st_ret == GST_STATE_CHANGE_FAILURE);
            fprintf(stderr, "Play done\n");
            /* Set as playing */
            pthread_mutex_unlock(&play_state_mutex);
            /* Set play time */
            gettimeofday(&time_play, 0);
            response.order = OK_RTP;
            response.server_port = rtp_port;
            st = send(rtsp_sockfd, &response, sizeof(RTP_TO_RTSP), 0);
            break;
        }
        case PAUSE_RTP: {
            GstStateChangeReturn st_ret;
            fprintf(stderr, "Recibido pause en proceso %d\n", getpid());
            /* TODO: Send pause command to gstreamer thread */
            /* TODO: goto error if error */
            /* Set as paused */
            pthread_mutex_lock(&play_state_mutex);
            do {

                GstState state;
                GstState pending;

                fprintf(stderr, "Trying pause in process %d\n", getpid());
                gst_element_set_state(pipeline, GST_STATE_PAUSED);

                st_ret = gst_element_get_state(pipeline, &state, &pending, GST_CLOCK_TIME_NONE);
                if (st_ret == GST_STATE_CHANGE_SUCCESS)
                    fprintf(stderr, "Successful pause\n");
                else if (st_ret == GST_STATE_CHANGE_FAILURE)
                    fprintf(stderr, "Error in pause\n");
            } while (st_ret == GST_STATE_CHANGE_ASYNC || st_ret == GST_STATE_CHANGE_FAILURE);
            /* Set pause time */
            gettimeofday(&time_pause, 0);
            response.order = OK_RTP;
            response.server_port = rtp_port;
            send(rtsp_sockfd, &response, sizeof(RTP_TO_RTSP), 0);
            break;
        }
        case TEARDOWN_RTP:
            /* NOTE: This isn't really necesary for the server to work */
            /* Send FINISHED_RTP to RTSP server */
            /*response.order = FINISHED_RTP;
            response.Session = message.message.Session;
            strcpy(response.uri, message.message.uri);
            response.ssrc = message.message.ssrc;
            send(rtsp_sockfd, &response, sizeof(response), 0); */
            /* TODO: goto error if error */
            fprintf(stderr, "Recibido teardown en proceso %d\n", getpid());
            response.order = OK_RTP;
            response.server_port = rtp_port;
            send(rtsp_sockfd, &response, sizeof(RTP_TO_RTSP), 0);
            goto terminate;
            break;
        default:
            break;
        }
        close(rtsp_sockfd);
        rtsp_sockfd = -1;
    }

    goto terminate;
terminate_error:
    /* Send ERR_RTP to rtsp socket */
    response.order = ERR_RTP;
    send(rtsp_sockfd, &response, sizeof(RTP_TO_RTSP), 0);
terminate:
    free_worker_process();
    /* Send die message so this process is waited for */
    die_message.mtype = getppid();
    die_message.pid = getpid();
    msgsnd(msg_queue, &die_message, sizeof(pid_t), 0);
    kill(getpid(), SIGKILL);
}
Example #5
0
int rtp_worker_create(int sockfd, struct sockaddr_storage *rtsp_socket) {
    int ret;
    RTSP_TO_RTP message;
    RTP_TO_RTSP response;
    RTP_WORKER *worker;
    struct msg_to_worker msg;
    char *host, *path;
    int st;
    pid_t child;
    int i;
    unsigned int *ssrc;
    /* TODO */
    ret = recv(sockfd, &message, sizeof(RTSP_TO_RTP), MSG_WAITALL);
    if (ret != sizeof(RTSP_TO_RTP))
        return -1;
    switch (message.order) {
    case CHECK_EXISTS_RTP:
        /* Extract path from uri */
        st = extract_uri(message.uri, &host, &path);
        if (st &&  host && path) {
            /* Check if the path exists in the computer */
            /* Ignore last /audio or /video */
            path[strlen(path)-6] = 0;
            st = check_file_exists(path);
            if (st)
                response.order = OK_RTP;
            else
                response.order = ERR_RTP;
        } else {
            response.order = ERR_RTP;
        }
        free(host);
        free(path);
        ret = send(sockfd, &response, sizeof(RTP_TO_RTSP), 0);
        break;
    case SETUP_RTP_UNICAST:
        /* Extract the path */
        st = extract_uri(message.uri, &host, &path);
        if (!st || !host || !path) {
            response.order = ERR_RTP;
            ret = send(sockfd, &response, sizeof(RTP_TO_RTSP), 0);
            return(0);
        }
        /* Check if the file exists */
        /* Ignore last /audio or /video */
        path[strlen(path)-6] = 0;
        st = check_file_exists(path);
        if (!st) {
            response.order = ERR_RTP;
            ret = send(sockfd, &response, sizeof(RTP_TO_RTSP), 0);
            free(host);
            free(path);
            return(0);
        }
        free(host);
        free(path);

        /* Create worker process */
        child = fork();
        if (child == 0) {
            rtp_worker_fun();
        } else if (child < 0) {
            response.order = ERR_RTP;
            ret = send(sockfd, &response, sizeof(RTP_TO_RTSP), 0);
            return(0);
        }

        pthread_mutex_lock(&workers_mutex);
        /* Check if we can have more workers */
        if (n_workers == MAX_RTP_WORKERS) {
            response.order = ERR_RTP;
            ret = send(sockfd, &response, sizeof(RTP_TO_RTSP), 0);
            kill(child, SIGKILL);
            waitpid(child, 0, 0);
            return(0);
        }
        /* Search for a free worker */
        for (i = 0; i < MAX_RTP_WORKERS; ++i)
            if (workers[i]->used == 0)
                break;
        /* Intialize RTP_WORKER structure */
        workers[i]->used = 1;
        workers[i]->pid = child;
        /* Create new ssrc */
        do {
            workers[i]->ssrc = rand();
        } while (gethashtable(&workers_hash, &(workers[i]->ssrc)));

        /* Reserve memory for worker */
        worker = malloc(sizeof(RTP_WORKER));
        if (!worker) {
            response.order = ERR_RTP;
            ret = send(sockfd, &response, sizeof(RTP_TO_RTSP), 0);
            kill(child, SIGKILL);
            waitpid(child, 0, 0);
            return(0);
        }
        /* Initialize worker */
        worker->pid = child;
        /* Insert worker in workers hash */
        ssrc = malloc(sizeof(unsigned int));
        if (!ssrc) {
            response.order = ERR_RTP;
            ret = send(sockfd, &response, sizeof(RTP_TO_RTSP), 0);
            kill(child, SIGKILL);
            waitpid(child, 0, 0);
            return(0);
        }
        *ssrc = workers[i]->ssrc;
        st = puthashtable(&workers_hash, ssrc, worker);
        if (st) {
            response.order = ERR_RTP;
            ret = send(sockfd, &response, sizeof(RTP_TO_RTSP), 0);
            kill(child, SIGKILL);
            waitpid(child, 0, 0);
            return(0);
        }
        /* Insert the ssrc in the message */
        message.ssrc = workers[i]->ssrc;

        pthread_mutex_unlock(&workers_mutex);

    /* Fall to default */
    default:
        pthread_mutex_lock(&workers_mutex);
        /* Get the worker pid from the ssrc we got in the message */
        worker = gethashtable(&workers_hash, &(message.ssrc));
        if (!worker) {
            response.order = ERR_RTP;
            ret = send(sockfd, &response, sizeof(RTP_TO_RTSP), 0);
            return(0);
        }

        /* Create message for worker */
        msg.mtype = worker->pid;
        msg.sockfd = sockfd;
        memcpy(&(msg.message), &message, sizeof(RTSP_TO_RTP));
        memcpy(&(msg.rtsp_socket), rtsp_socket, sizeof(struct sockaddr_storage));

        /* Send to the message to the worker */
        st = msgsnd(msg_queue, &msg, sizeof(struct msg_to_worker) - sizeof(long), 0);
        if (st == -1) {
            response.order = ERR_RTP;
            ret = send(sockfd, &response, sizeof(RTP_TO_RTSP), 0);
            return(0);
        }

        pthread_mutex_unlock(&workers_mutex);
        /* Now the worker must get the message and communicate with the RTSP server. */
        break;
    }
    return(1);
}