Socket HTTP post request
This was working fine, but it just seems hard-coded and wrong.


It's a monolythic monster. It works, but if you have the time, you could benefit from re-writing it (using c++ alternatives).


  How can I make the size of FormBuffer to be dynamic instead of 1024?


Like so (C++):

bool SendBasicInfo(USER* user, SOCKET sock)
{
    std::ostringstream FormBuffer; // &lt;&lt;&lt; here

    char DataType1[] = "a=";
    char DataType2[] = "&amp;b=";
    char DataType3[] = "&amp;c=";

    char FormAction[] = "/g/g.php";

    // get: length of the actual content
    auto ContentLength = strlen(user-&gt;user_id) + 
        strlen(user-&gt;pcname) + 
        strlen(user-&gt;username) +
        strlen(DataType1) +
        strlen(DataType2) +
        strlen(DataType3);

    // header
    FormBuffer &lt;&lt; "POST " &lt;&lt; FormAction &lt;&lt; " HTTP/1.1\n";
    FormBuffer &lt;&lt; "Content-Type: application/x-www-form-urlencoded\n";
    FormBuffer &lt;&lt; "Host: \n";
    FormBuffer &lt;&lt; "Content-Length: " &lt;&lt; std::to_string(ContentLength) &lt;&lt; "\n\n";

    // actual content
    FormBuffer &lt;&lt; DataType1 &lt;&lt; user-&gt;user_id;
    FormBuffer &lt;&lt; DataType2 &lt;&lt; user-&gt;pcname;
    FormBuffer &lt;&lt; DataType3 &lt;&lt; user-&gt;username;

    const auto str = FormBuffer.str();

    std::cout &lt;&lt; str &lt;&lt; std::endl;

    return ( send(sock, str.data(), str.size(), NULL) &gt;= str.size() );
}


Otherwise (if you want to write C code), you will have to iterate over the entire data once and compute it's resulting size, then allocate memory for the result, then iterate again over the data to create the buffer contents, then sent to socket and remember the result in a variable, then free the allocated memory, then deturn the stored result. If it sounds ugly, that's because it is :D.

Edit: The HTTP standard states that you should send \r\n at the end of header lines, not \n. It may not matter much in effect though, because both servers and clients are mandated to receive and parse both variants - as developers tend not to respect this rule (I may be remembering it wrong though).