Correctly using gumbo-parser to iterate and find the things I need?

Viewed 2146

I'm working on a pure C99 practising project which can do a login simulation for our school's CAS login system.

Now I'm trying to use Gumbo HTML parser to parse our school's login page. Here is the form section and I need to get the login ticket from it before I run the POST request to submit the form, which is the "hidden" type input element with name "lt". (i.e. line with <input type="hidden" name="lt" value="LT-000000-b4LktCXyzXyzXyzXyzXyzXyz" />, and I need to parse the "value").

I've wrote some code, but it seems cannot find out this input element. Here is my C program's function:

const char * parse_login_ticket_old(char * raw_html)
{
    // Parse HTML into Gumbo memory structure
    GumboOutput * gumbo_output = gumbo_parse(raw_html);

    // Prepare the node
    GumboNode * gumbo_root = gumbo_output->root;

    assert(gumbo_root->type == GUMBO_NODE_ELEMENT);
    assert(gumbo_root->v.element.children.length >= 2);

    const GumboVector* root_children = &gumbo_root->v.element.children;

    GumboNode* page_body = NULL;


    for (int i = 0; i < root_children->length; ++i)
    {
        GumboNode* child = root_children->data[i];
        if (child->type == GUMBO_NODE_ELEMENT && child->v.element.tag == GUMBO_TAG_BODY)
        {
            page_body = child;
            break;
        }
    }

    assert(page_body != NULL);

    GumboVector* page_body_children = &page_body->v.element.children;

    for (int i = 0; i < page_body_children->length; ++i)
    {
        GumboNode* child = page_body_children->data[i];

        GumboAttribute * input_name_attr = gumbo_get_attribute(&child->v.element.attributes, "name");

        if (child->type == GUMBO_NODE_ELEMENT && child->v.element.tag == GUMBO_TAG_INPUT && strcmp(input_name_attr->value, "lt") == 0)
        {
            GumboAttribute * input_value_attr = gumbo_get_attribute(&child->v.element.attributes, "value");
            return input_name_attr->value;
        }
    }

    return NULL;
}

In case someone need for debugging, here is a example of our schools page. Possible sensitive data has been removed.

<body>
    <div id="wrapper">
        <div id="contentArea" role="main">
            <div class="form login" role="form">
                <h2 class="hidden">Login</h2>
                <form id="fm1" class="fm-v clearfix" action="/schoolcas/login?jsessionid=1234567890" method="post"><div class="formRow">
                        <label for="username" class="label">Student ID</label>
                        <div class="textBox">
                            <input id="username" name="username" class="schoolcas text" aria-required="true" type="text" value="" size="25" maxlength="25"/></div>
                    </div>
                    <div class="formRow">
                        <label for="password" class="label">Password</label>
                        <div class="textBox">
                            <input id="password" name="password" class="schoolcas text" aria-required="true" type="password" value="" size="25" autocomplete="off"/></div>
                    </div>

                    <div class="formRow">
                        <input type="hidden" name="lt" value="LT-000000-b4LktCXyzXyzXyzXyzXyzXyz" />
                        <input type="hidden" name="execution" value="e2s1" />
                        <input type="hidden" name="_eventId" value="submit" />
                        <input class="button grey submit" name="submit" value="Login" type="submit" />
                    </div>

                </form>
            </div>
        </div>
    </div>
</body>

Anyway, my program seems just stop at the top of the body element and it returns NULL later on.

So I would like to know how to do a correct search, and find out the input element I need?

1 Answers
Related