error in sending post to log in google account

Discussion in 'C Programming' started by lanclot, Nov 15, 2007.

  1. lanclot

    lanclot Guest

    hi
    i am trying to authenticate a user using ClientLogin by sending a
    post to https://www.google.com/accounts/ClientLogin
    in my c program use ssl and socket
    the next code is my request:
    sprintf(request, "POST /accounts/ClientLogin HTTP/1.0\r\nContent-type:
    application/x-www-form-urlencoded\r\nContent-length:1357\r
    \nEmail=&Passwd=mypasswd&service=lh2&source=sd-aa-1.0\r
    \n");

    but the response is null

    i do not known what's wrong, if you have some advise, email me or
    response
    thanks
    lanclot, Nov 15, 2007
    #1
    1. Advertising

  2. lanclot

    Jack Klein Guest

    On Wed, 14 Nov 2007 17:57:08 -0800 (PST), lanclot
    <> wrote in comp.lang.c:

    > hi
    > i am trying to authenticate a user using ClientLogin by sending a
    > post to https://www.google.com/accounts/ClientLogin
    > in my c program use ssl and socket


    SSL and socket are not part of the C language or its library. They
    are extensions to the language, and off-topic here.

    > the next code is my request:
    > sprintf(request, "POST /accounts/ClientLogin HTTP/1.0\r\nContent-type:
    > application/x-www-form-urlencoded\r\nContent-length:1357\r
    > \nEmail=&Passwd=mypasswd&service=lh2&source=sd-aa-1.0\r
    > \n");


    This sprintf() call will create an exact copy of the string literal in
    the array of characters pointed to by "request", assuming "request" is
    a pointer to char and it points to enough non-const chars.

    If "request" a pointer to enough characters? Does it contain what you
    expect it to contain after the function call?

    > but the response is null


    You haven't told us where the response comes from, so far you have
    only shown us a call to sprintf() that might or might not be correct,
    depending on the definition of "request" and of whatever "request"
    points to.

    > i do not known what's wrong, if you have some advise, email me or
    > response
    > thanks


    You need to post this to a group that supports your compiler/operating
    system combination, and most likely you need to show more code, like
    what it does after the sprintf() call.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Nov 15, 2007
    #2
    1. Advertising

  3. lanclot

    lanclot Guest

    i am sorry for my list.
    i have not given enough information for my question.
    my os is fedora use gcc compile
    i am trying to write a application to login google picasa, now i
    will give more code for it
    thanks for your attention
    if (connect(sockfd, (struct sockaddr *) (&server_addr),
    sizeof(struct sockaddr)) == -1) { /*Á¬½ÓÍøÕ¾ */
    if (DEBUG)
    fprintf(stderr, "Connect Error:%s\a\n", strerror(errno));
    exit(1);
    }

    /* SSL³õʼ»¯ */
    SSL_library_init();
    SSL_load_error_strings();
    ctx = SSL_CTX_new(SSLv23_client_method());
    if (ctx == NULL) {
    ERR_print_errors_fp(stderr);
    exit(1);
    }

    ssl = SSL_new(ctx);
    if (ssl == NULL) {
    ERR_print_errors_fp(stderr);
    exit(1);
    }

    /* °ÑsocketºÍSSL¹ØÁª */
    ret = SSL_set_fd(ssl, sockfd);
    if (ret == 0) {
    ERR_print_errors_fp(stderr);
    exit(1);
    }

    RAND_poll();
    while (RAND_status() == 0) {
    unsigned short rand_ret = rand() % 65536;
    RAND_seed(&rand_ret, sizeof(rand_ret));
    }

    ret = SSL_connect(ssl);
    if (ret != 1) {
    ERR_print_errors_fp(stderr);
    exit(1);
    }
    sprintf(request, "POST /accounts/ClientLogin HTTP/1.0\r\nContent-
    length:1344\r\nContent-type: application/x-www-form-urlencoded\r
    \naccountType=HOSTED_OR_GOOGLE&Email=&Passwd=031126&service=lh2&source=sd4-
    aa-1.0\r\nHost: %s:%d\r\nConnection: Close\r\n\r\n",
    host_addr,portnumber);

    if (DEBUG)
    printf("%s", request); /* prepare request£¬ */


    if (host_file && *host_file)
    pt = Rstrchr(host_file, '/');
    else
    pt = 0;

    memset(local_file, 0, sizeof(local_file));
    if (pt && *pt) {
    if ((pt + 1) && *(pt + 1))
    strcpy(local_file, pt + 1);
    else
    memcpy(local_file, host_file, strlen(host_file) - 1);
    } else if (host_file && *host_file)
    strcpy(local_file, host_file);
    else
    strcpy(local_file, "index.html");
    if (DEBUG)
    printf("local filename to write:%s\r\n", local_file);

    /send https request */
    send = 0;
    totalsend = 0;
    nbytes = strlen(request);
    while (totalsend < nbytes) {
    send = SSL_write(ssl, request + totalsend, nbytes -
    totalsend);
    if (send == -1) {
    if (DEBUG)
    ERR_print_errors_fp(stderr);
    exit(0);
    }
    totalsend += send;
    if (DEBUG)
    printf("%d bytes send OK!\n", totalsend);
    }

    fp = fopen(local_file, "a");
    if (!fp) {
    if (DEBUG)
    printf("create file error! %s\n", strerror(errno));
    return 0;
    }
    if (DEBUG)
    printf("\nThe following is the response header:\n");
    i = 0;
    /* connected£¬response https */
    while ((nbytes = SSL_read(ssl, buffer, 1)) == 1) {
    if (i < 4) {
    if (buffer[0] == '\r' || buffer[0] == '\n')
    i++;
    else
    i = 0;
    if (DEBUG)
    printf("%c", buffer[0]); /*print https header
    info */
    } else {
    fwrite(buffer, 1, 1, fp);
    i++;
    if (i % 1024 == 0)
    fflush(fp);
    }
    }
    fclose(fp);
    lanclot, Nov 15, 2007
    #3
  4. lanclot wrote:
    >
    > hi
    > i am trying to authenticate a user using ClientLogin by sending a
    > post to https://www.google.com/accounts/ClientLogin
    > in my c program use ssl and socket
    > the next code is my request:
    > sprintf(request, "POST /accounts/ClientLogin HTTP/1.0\r\nContent-type:
    > application/x-www-form-urlencoded\r\nContent-length:1357\r
    > \nEmail=&Passwd=mypasswd&service=lh2&source=sd-aa-1.0\r
    > \n");
    >
    > but the response is null
    >
    > i do not known what's wrong, if you have some advise, email me or
    > response


    Well, SSL, sockets, HTTP, and so on are off-topic here. However,
    is it possible that the fact that you are putting both a '\r' and
    a '\n' to mark EOL cause you problems?

    Beyond that, you'll need to ask somewhere that can answer questions
    on your SSL library.

    (And if that is really a valid email address and password that you
    posted in your followup, I would suggest that you change the password
    ASAP.)

    --
    +-------------------------+--------------------+-----------------------+
    | Kenneth J. Brody | www.hvcomputer.com | #include |
    | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------+
    Don't e-mail me at: <mailto:>
    Kenneth Brody, Nov 15, 2007
    #4
  5. lanclot

    Jack Klein Guest

    On Wed, 14 Nov 2007 21:33:29 -0800 (PST), lanclot
    <> wrote in comp.lang.c:

    > i am sorry for my list.
    > i have not given enough information for my question.
    > my os is fedora use gcc compile
    > i am trying to write a application to login google picasa, now i
    > will give more code for it
    > thanks for your attention


    [snip]

    You missed the most important point of my reply, and of Kenneth's as
    well.

    There are no sockets or SSL in standard C, so we do not talk about
    them here.

    The libraries you are using are extensions to the language provided by
    your compiler and operating system.

    Now that you have specified what these are, I can tell you the correct
    place to ask for help: news:comp.os.linux.development.apps

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Nov 16, 2007
    #5
  6. On Wed, 14 Nov 2007 21:33:29 -0800 (PST), lanclot
    <> wrote:

    <snip>
    > sprintf(request, "POST /accounts/ClientLogin HTTP/1.0\r\nContent-
    > length:1344\r\nContent-type: application/x-www-form-urlencoded\r
    > \naccountType=HOSTED_OR_GOOGLE&Email=&Passwd=031126&service=lh2&source=sd4-
    > aa-1.0\r\nHost: %s:%d\r\nConnection: Close\r\n\r\n",
    > host_addr,portnumber);
    >

    <ontopic>
    You still haven't shown the declaration and if applicable allocation
    of request. Unless it points to a sufficient number of writable char's
    for the data sprintf puts there, you have Undefined Behavior.
    <offtopic>
    If it does, you have a bogus HTTP/1.0 request.

    Most seriously you have data intermixed with the headers. POST data
    must follow the end of header (\r\n\r\n *) and content-length's value
    must give its length; your data here is nowhere near 1344 chars=bytes.
    (* Technically, CR LF CR LF; like many Internet protocols HTTP is
    defined in ASCII or an extension thereof, while C does not require
    ASCII so \n and \r in C could actually be newline and return in some
    different character set, and not the correct codes for HTTP -- but not
    on systems you're likely to use.)

    Also, Host: is not defined for 1.0, and if the server supports it as
    an extension compatible with 1.1 putting an _address_ in it rather
    than a (domain) name defeats the purpose for which it was defined.

    Finally, Connection: is not standard in 1.0 and the default is already
    equivalent to connection=close.
    </>

    > if (DEBUG)
    > printf("%s", request); /* prepare request£¬ */
    >
    >

    Minor point: requests with body don't need to end with an end-of-line
    (\n in C), so you probably want to add one at least conditionally.
    Formally C allows an implementation to not support/allow text files
    where the last line doesn't end with a newline; in practice most do,
    almost certainly including yours, but it's still confusing and ugly.

    > if (host_file && *host_file)
    > pt = Rstrchr(host_file, '/');
    > else
    > pt = 0;
    >

    If (nonstandard) Rstrchr doesn't do something very much like the
    standard strchr, it is confusingly named. If it does ...

    > memset(local_file, 0, sizeof(local_file));
    > if (pt && *pt) {


    .... this test is redundant; if pt was set to point to a character in a
    string then both pt != NULL and *pt != '\0'; if it was set to 0 then
    pt == NULL (and *pt must not be and is not tested).

    > if ((pt + 1) && *(pt + 1))


    .... and so is this. If pt points to a character in a string and is
    necessarily nonnull, then pt+1 is also nonnull. Checking *(pt+1) is
    useful, although pt[1] is equivalent and arguably more idiomatic.

    > strcpy(local_file, pt + 1);
    > else
    > memcpy(local_file, host_file, strlen(host_file) - 1);


    You really want to use all but the last character of (the contents of
    or value pointed to by) host_file? That seems odd.

    <snip>
    > i = 0;
    > /* connected£¬response https */
    > while ((nbytes = SSL_read(ssl, buffer, 1)) == 1) {


    <semi-offtopic> Calling SSL_read for each character will probably
    waste quite a bit of CPU, at least assuming this is openssl as it
    appears. (Openssl in particular is offtopic, but IMO the general
    concept of few large calls versus many small ones is ontopic.) </>

    > if (i < 4) {
    > if (buffer[0] == '\r' || buffer[0] == '\n')
    > i++;
    > else
    > i = 0;
    > if (DEBUG)
    > printf("%c", buffer[0]); /*print https header
    > info */
    > } else {
    > fwrite(buffer, 1, 1, fp);
    > i++;
    > if (i % 1024 == 0)
    > fflush(fp);
    > }
    > }

    This doesn't make sense. It appears you are trying to use the same
    variable, i, both to count data received/written and count consecutive
    \r and \n to find end-of-header. Those are conflicting uses.

    Also, flushing output every 1024 bytes is dubious. 1024 doesn't have
    any particular significance to the HTTP format, except by the remotest
    chance; and while it might matter to the buffering done by stdio,
    stdio will already flush whenever it needs to based on its (actual)
    buffering without you even thinking about it -- that's one of the
    reasons for having and using stdio in the first place.

    > fclose(fp);

    - formerly david.thompson1 || achar(64) || worldnet.att.net
    David Thompson, Nov 25, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. nilapenn
    Replies:
    3
    Views:
    607
    Joe Kaplan \(MVP - ADSI\)
    Feb 14, 2005
  2. Replies:
    4
    Views:
    623
    Paul Clement
    Sep 15, 2005
  3. Aredridel
    Replies:
    3
    Views:
    91
    Brian Mitchell
    Dec 10, 2004
  4. Kevin Lee
    Replies:
    1
    Views:
    201
    Peter Szinek
    Jun 11, 2007
  5. Wesley Chen
    Replies:
    0
    Views:
    97
    Wesley Chen
    Apr 9, 2009
Loading...

Share This Page