2010-11-22 109 views
0

我想使用LibCurl将此代码(使用带Sockets扩展的脚本语言)转换为C++。我以前只使用了LibCurl,所以我对于我需要的其他东西有点遗憾。我的主要问题是,我应该只能使用curl_easy_setopt(curl,CURLOPT_HTTPHEADER,headers);发送这个,或者如果我需要提取套接字,然后发送它。C++ LibCurl从套接字获取

这是从剧本的相关片段...

public OnSocketConnected(Handle:socket, any:friendId) 
{ 
    decl String:CommunityId[32]; 
    FriendIDToCommunityId(friendId, CommunityId, sizeof(CommunityId)); 

    decl String:query[2048]; 
    decl String:cookieString[100]; 
    decl String:inviterString[32]; 
    decl String:groupString[32]; 

    GetConVarString(cookie, cookieString, sizeof(cookieString)); 
    GetConVarString(inviter, inviterString, sizeof(inviterString)); 
    GetConVarString(group, groupString, sizeof(groupString)); 


    Format(query, sizeof(query), "GET /actions/GroupInvite?type=groupInvite&inviter=%s&invitee=%s&group=%s HTTP/1.1\r\nHost: steamcommunity.com\r\nConnection: close\r\nCookie: steamLogin=%s\r\n\r\n", inviterString, CommunityId, groupString, cookieString); 
    SocketSend(socket, query); 
    LogMessage("%s", query); 
} 

和这里是我在C++至今。它看起来像我需要首先提取套接字,但我不熟练使用网络编码,所以我不确定我需要从这里走到哪里。

void InviteToGroup(const char *szUserSteamID, const char *szInviterSteamID, const char *steamUser, const char *steamPass) 
{ 
    CURL *curl; 
    CURLcode res, result; 
    //int sockfd; /* socket */ 
    char errorBuffer[CURL_ERROR_SIZE]; 

    const char *szUserID = GetCommunityID(szUserSteamID); // User's Steam Community ID 
    const char *szInviterID = GetCommunityID(szInviterSteamID); // Inviter's Steam Community ID 
    char *szGroupID = ""; 
    GetGroupCommunityID(1254745, &szGroupID); // Group Steam Community ID 
    const char *szCookie = "76561198018111441%7C%7CC7D70E74A3F592F3E130CCF4CAACD4A7B9CAD993"; // Steam Community Login Cookie 

    char *buffer = new char[512]; 

    // Create the GET request 
    struct curl_slist *headers = NULL; 
    headers = curl_slist_append(headers, "GET /actions/GroupInvite?type=groupInvite&inviter="); 
    snprintf(buffer, sizeof(buffer), "%s&invitee=%s&group=%s ", szInviterID, szUserID, szGroupID); 
    headers = curl_slist_append(headers, buffer); 
    headers = curl_slist_append(headers, "HTTP/1.1\r\nHost: steamcommunity.com\r\nConnection: close\r\nCookie: "); 
    snprintf(buffer, sizeof(buffer), "steamLogin=%s\r\n\r\n", szCookie); 
    headers = curl_slist_append(headers, buffer); 

    delete buffer; 

    // Init CURL 
    curl = curl_easy_init(); 

    if(curl) 
    { 
     curl_easy_setopt(curl, CURLOPT_URL, "https://www.steamcommunity.com"); 
     curl_easy_setopt(curl, CURLOPT_PORT, 443); // Check this before using 
     curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer); 
     //curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1); // No transfer, just extract the socket 

     // Find out if we need to use Proxy stuff here 

     char *userpass = new char[64]; 
     snprintf(userpass, sizeof(userpass), "%s:%s", steamUser, steamPass); 
     curl_easy_setopt(curl, CURLOPT_USERPWD, userpass); 
     curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); 

     // Attempt to Connect the Steam Community Server 
     res = curl_easy_perform(curl); 

     if (res == CURLE_OK) 
      Msg("Connected Successfully!\n"); 
     else 
      Msg("Connection Failed! Error: %s\n", errorBuffer); 

     // Close the connection 
     curl_easy_cleanup(curl); 
    } 
} 

回答

0

因此,在这里更正为您的C++不正确,这将意味着您的代码将无法工作。

sizeof(char *)是指针的大小,而不是字符串的长度。因此,您的snprintf将在32位系统上打印最多4个字符,其中包含空终止符,因此您实际上只能得到长度为3的字符串。

如果您要使用“应该足够大”缓冲区不分配新的。如果你想使用snprintf,你可以使用std :: vector,然后检查snprintf的返回值,它是将被写入的实际字符串的长度,然后在必要时增加缓冲区大小(返回值+1)。

vector<char> buffer; 
size_t required = 511; 
do 
{ 
    buffer.resize(required + 1); 
    required = snprintf(&buffer[0], buffer.size(), formatstr, ...); 
} 
while(required >= buffer.size()); 

无需在结束时“释放”缓冲区(顺便说一句,您做错了),因为向量会自动执行该操作。

现在,您将& buffer [0]传递给该函数,它将指向以空字符结尾的字符串,并且如果API需要char *而不是const char *,则该字符串将是可写的。 (libcurl虽然需要...作为curl_easy_setopt的第三个参数,所以你可以传入“任何东西”,所以你必须小心,因为如果你不小心传入了std :: string或的std ::向量)。