示例#1
0
文件: PubNub.c 项目: Mazetti/asf
static int parse_subscribe_response(pubnub_t *p)
{
    char *reply = p->http_reply;
    unsigned int replylen = strlen(reply);
    if (reply[replylen-1] != ']' && replylen > 2) {
        replylen -= 2; // XXX: this seems required by Manxiang
    }
    if ((reply[0] != '[') || (reply[replylen-1] != ']') || (reply[replylen-2] != '"')) {
        return -1;
    }

    /* Extract the last argument. */
    int i = find_string_start(reply, replylen-2);
    if (i < 0) {
        return -1;
    }
    reply[replylen - 2] = 0;

    /* Now, the last argument may either be a timetoken or a channel list. */
    if (reply[i-2] == '"') {
        int k;
        /* It is a channel list, there is another string argument in front
         * of us. Process the channel list ... */
        p->chan_ofs = i+1;
        p->chan_end = replylen - 1;
        for (k = p->chan_end - 1; k > p->chan_ofs; --k) {
            if (reply[k] == ',') {
                reply[k] = 0;
	    }
	}

        /* ... and look for timetoken again. */
	reply[i-2] = 0;
        i = find_string_start(reply, i-2);
        if (i < 0) {
            return -1;
        }
    } 
    else {
        p->chan_ofs = 0;
        p->chan_end = 0;
    }

    /* Now, i points at
     * [[1,2,3],"5678"]
     * [[1,2,3],"5678","a,b,c"]
     *          ^-- here */

    /* Setup timetoken. */
    if (replylen >= sizeof(p->timetoken) + 2 + (i+1)) {
        return -1;
    }
    strcpy(p->timetoken, reply + i+1);
    reply[i-2] = 0; // terminate the [] message array (before the ]!)

    /* Set up the message list - offset, length and NUL-characters splitting
     * the messages. */
    p->msg_ofs = 2;
    p->msg_end = i-2;

    return split_array(reply + p->msg_ofs) ? 0 : -1;
}
示例#2
0
int pbcc_parse_subscribe_response(struct pbcc_context *p)
{
    int i;
	int previous_i;
	unsigned time_token_length;
    char *reply = p->http_reply;
    int replylen = p->http_buf_len;
    if (replylen < 2) {
        return -1;
    }
    if (reply[replylen-1] != ']' && replylen > 2) {
        replylen -= 2; /* XXX: this seems required by Manxiang */
    }
    if ((reply[0] != '[') || (reply[replylen-1] != ']') || (reply[replylen-2] != '"')) {
        return -1;
    }

    /* Extract the last argument. */
	previous_i = replylen - 2;
    i = find_string_start(reply, previous_i);
    if (i < 0) {
        return -1;
    }
    reply[replylen - 2] = 0;

    /* Now, the last argument may either be a timetoken, a channel group list
		or a channel list. */
    if (reply[i-2] == '"') {
        int k;
		/* It is a channel list, there is another string argument in front
         * of us. Process the channel list ... */
        for (k = replylen - 2; k > i+1; --k) {
            if (reply[k] == ',') {
                reply[k] = '\0';
            }
        }

        /* The previous argument is either a timetoken or a channel group
			list. */
        reply[i-2] = '\0';
        p->chan_ofs = i+1;
        p->chan_end = replylen - 1;
		previous_i = i-2;
        i = find_string_start(reply, previous_i);
        if (i < 0) {
            p->chan_ofs = 0;
            p->chan_end = 0;
            return -1;
        }
		if (reply[i-2] == '"') {
			/* It is a channel group list. For now, we shall skip it. In
				the future, we may process it like we do the channel list.
				*/
			reply[i-2] = '\0';
			previous_i = i-2;
			i = find_string_start(reply, previous_i);
			if (i < 0) {
				return -1;
			}
		}
    }
    else {
        p->chan_ofs = 0;
        p->chan_end = 0;
    }

    /* Now, `i` points to:
     * [[1,2,3],"5678"]
     * [[1,2,3],"5678","a,b,c"]
     * [[1,2,3],"5678","gr-a,gr-b,gr-c","a,b,c"]
     *          ^-- here */

    /* Setup timetoken. */
	time_token_length = previous_i - (i+1);
	if (time_token_length >= sizeof p->timetoken) {
		p->timetoken[0] = '\0';
		return -1;
	}
	memcpy(p->timetoken, reply + i+1, time_token_length+1);
	
	/* terminate the [] message array (before the `]`!) */
    reply[i-2] = 0; 

    /* Set up the message list - offset, length and NUL-characters
     * splitting the messages. */
    p->msg_ofs = 2;
    p->msg_end = i-2;

    return split_array(reply + p->msg_ofs) ? 0 : -1;
}