Changeset 62f6b45


Ignore:
Timestamp:
2012-11-11T16:53:56Z (12 years ago)
Author:
Wilmer van der Gaast <wilmer@…>
Branches:
master
Children:
1388d30
Parents:
dff0e0b
Message:

Realised I don't need delimited=length at all, objects are guaranteed to be
CRLF-terminated.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • protocols/twitter/twitter_lib.c

    rdff0e0b r62f6b45  
    732732{
    733733        struct im_connection *ic = req->data;
    734         //struct twitter_data *td = ic->proto_data;
    735734        json_value *parsed;
    736         int len, i;
    737         char c;
     735        int len = 0;
     736        char c, *nl;
    738737       
    739738        if (!g_slist_find(twitter_connections, ic))
    740739                return;
    741         //td = ic->proto_data;;
    742740       
    743741        printf( "%d bytes in stream\n", req->body_size );
    744742       
    745         /* m/^[\d\s]*/      /* why is there a second commend here? :-) */
    746         for (i = 0; i < req->body_size; i ++) {
    747                 if (!isspace(req->reply_body[i]) && !isdigit(req->reply_body[i]))
    748                         break;
    749         }
    750        
    751         /* Nothing but numbers and whitespace in there. Try again later. */
    752         if (i == req->body_size)
    753                 return;
    754        
    755         /* Get length. */
    756         if (sscanf(req->reply_body, "%d", &len) != 1)
    757                 return;
    758        
    759         if (req->body_size < i + len) {
    760                 printf("Not enough bytes in buffer yet\n");
    761                 return;
    762         }
    763        
    764         http_flush_bytes(req, i);
    765         c = req->reply_body[len];
    766         req->reply_body[len] = '\0';
    767        
    768         printf("JSON: %s\n", req->reply_body);
    769         printf("parsed: %p\n", (parsed = json_parse(req->reply_body)));
    770         if (parsed) {
    771                 twitter_stream_handle_object(ic, parsed);
    772         }
    773         json_value_free(parsed);
    774         req->reply_body[len] = c;
    775        
    776         http_flush_bytes(req, len);
     743        /* MUST search for CRLF, not just LF:
     744           https://dev.twitter.com/docs/streaming-apis/processing#Parsing_responses */
     745        nl = strstr(req->reply_body, "\r\n");
     746       
     747        if (!nl) {
     748                printf("Incomplete data\n");
     749                return;
     750        }
     751       
     752        len = nl - req->reply_body;
     753        if (len > 0) {
     754                c = req->reply_body[len];
     755                req->reply_body[len] = '\0';
     756               
     757                printf("JSON: %s\n", req->reply_body);
     758                printf("parsed: %p\n", (parsed = json_parse(req->reply_body)));
     759                if (parsed) {
     760                        twitter_stream_handle_object(ic, parsed);
     761                }
     762                json_value_free(parsed);
     763                req->reply_body[len] = c;
     764        }
     765       
     766        http_flush_bytes(req, len + 2);
    777767       
    778768        /* One notification might bring multiple events! */
     
    799789{
    800790        struct twitter_data *td = ic->proto_data;
    801         char *args[4] = {"with", "followings", "delimited", "length"};
     791        char *args[2] = {"with", "followings"};
    802792       
    803793        if ((td->stream = twitter_http(ic, TWITTER_USER_STREAM_URL,
    804                                        twitter_http_stream, ic, 0, args, 4))) {
     794                                       twitter_http_stream, ic, 0, args, 2))) {
    805795                /* This flag must be enabled or we'll get no data until EOF
    806796                   (which err, kind of, defeats the purpose of a streaming API). */
Note: See TracChangeset for help on using the changeset viewer.