tRemove unused code - vaccinewars - be a doctor and try to vaccinate the world
(HTM) git clone git://src.adamsgaard.dk/vaccinewars
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
(DIR) commit a805134e497edaa59dac6cc2dcd61c6da0fd4bde
(DIR) parent 486a69322399619e18c17c02b26f8d7e59f5c43f
(HTM) Author: Ben Webb <ben@salilab.org>
Date: Fri, 6 Nov 2020 19:25:03 -0800
Remove unused code
Now that we use libcurl for all HTTP work,
we don't need our own functions any more.
Diffstat:
M src/curses_client/curses_client.c | 30 ------------------------------
M src/message.c | 23 -----------------------
M src/network.c | 431 -------------------------------
M src/network.h | 63 -------------------------------
4 files changed, 0 insertions(+), 547 deletions(-)
---
(DIR) diff --git a/src/curses_client/curses_client.c b/src/curses_client/curses_client.c
t@@ -102,8 +102,6 @@ static void scroll_msg_area_down(void);
#ifdef NETWORKING
-static void HttpAuthFunc(HttpConnection *conn, gboolean proxyauth,
- gchar *realm, gpointer data);
static void SocksAuthFunc(NetworkBuffer *netbuf, gpointer data);
#endif
t@@ -538,34 +536,6 @@ static void DisplayConnectStatus(NetworkBuffer *netbuf,
g_string_free(text, TRUE);
}
-void HttpAuthFunc(HttpConnection *conn, gboolean proxyauth,
- gchar *realm, gpointer data)
-{
- gchar *text, *user, *password = NULL;
-
- attrset(TextAttr);
- clear_bottom();
- if (proxyauth) {
- text = g_strdup_printf(_("Proxy authentication required for realm %s"),
- realm);
- } else {
- text =
- g_strdup_printf(_("Authentication required for realm %s"), realm);
- }
- mvaddstr(17, 1, text);
- mvaddstr(18, 1, _("(Enter a blank username to cancel)"));
- g_free(text);
-
- user = nice_input(_("User name: "), 19, 1, FALSE, NULL, '\0');
- if (user && user[0]) {
- password = nice_input(_("Password: "), 20, 1, FALSE, NULL, '*');
- }
-
- SetHttpAuthentication(conn, proxyauth, user, password);
- g_free(user);
- g_free(password);
-}
-
void SocksAuthFunc(NetworkBuffer *netbuf, gpointer data)
{
gchar *user, *password = NULL;
(DIR) diff --git a/src/message.c b/src/message.c
t@@ -406,29 +406,6 @@ gboolean WritePlayerDataToWire(Player *Play)
return WriteDataToWire(&Play->NetBuf);
}
-typedef enum {
- MEC_INTERNAL,
- MEC_BADREPLY
-} MetaErrorCode;
-
-static void MetaAppendError(GString *str, LastError *error)
-{
- switch (error->code) {
- case MEC_INTERNAL:
- g_string_append_printf(str, _("Internal metaserver error \"%s\""),
- (gchar *)error->data);
- break;
- case MEC_BADREPLY:
- g_string_append_printf(str, _("Bad metaserver reply \"%s\""),
- (gchar *)error->data);
- break;
- default:
- g_string_append_printf(str, _("Unknown metaserver error code %d"),
- error->code);
- break;
- }
-}
-
gboolean OpenMetaHttpConnection(CurlConnection *conn, GError **err)
{
gboolean ret;
(DIR) diff --git a/src/network.c b/src/network.c
t@@ -395,72 +395,6 @@ static void SocksAppendError(GString *str, LastError *error)
static ErrorType ETSocks = { SocksAppendError, NULL };
-typedef enum {
- HEC_TRIESEX = 1,
- HEC_BADAUTH,
- HEC_BADREDIR,
- HEC_BADSTATUS,
- HEC_OK = 200,
- HEC_REDIRECT = 300,
- HEC_MOVEPERM = 301,
- HEC_MOVETEMP = 302,
- HEC_CLIENTERR = 400,
- HEC_AUTHREQ = 401,
- HEC_PROXYAUTH = 407,
- HEC_FORBIDDEN = 403,
- HEC_NOTFOUND = 404,
- HEC_SERVERERR = 500
-} HTTPErrorCode;
-
-static void HTTPAppendError(GString *str, LastError *error)
-{
- switch (error->code) {
- case HEC_TRIESEX:
- /* Various HTTP error messages */
- g_string_append(str, _("Number of tries exceeded"));
- break;
- case HEC_BADAUTH:
- g_string_append_printf(str, _("Bad auth header: %s"), (gchar *)error->data);
- break;
- case HEC_BADREDIR:
- g_string_append_printf(str, _("Bad redirect: %s"), (gchar *)error->data);
- break;
- case HEC_BADSTATUS:
- g_string_append_printf(str, _("Invalid HTTP status line: %s"),
- (gchar *)error->data);
- break;
- case HEC_FORBIDDEN:
- g_string_append(str, _("403: forbidden"));
- break;
- case HEC_NOTFOUND:
- g_string_append(str, _("404: page not found"));
- break;
- case HEC_AUTHREQ:
- g_string_append(str, _("401: HTTP authentication failed"));
- break;
- case HEC_PROXYAUTH:
- g_string_append(str, _("407: HTTP proxy authentication failed"));
- break;
- case HEC_MOVETEMP:
- case HEC_MOVEPERM:
- g_string_append(str, _("Bad redirect message from server"));
- break;
- default:
- if (error->code < HEC_REDIRECT || error->code >= 600) {
- g_string_append_printf(str, _("Unknown HTTP error %d"), error->code);
- } else if (error->code < HEC_CLIENTERR) {
- g_string_append_printf(str, _("%d: redirect error"), error->code);
- } else if (error->code < HEC_SERVERERR) {
- g_string_append_printf(str, _("%d: HTTP client error"), error->code);
- } else {
- g_string_append_printf(str, _("%d: HTTP server error"), error->code);
- }
- break;
- }
-}
-
-static ErrorType ETHTTP = { HTTPAppendError, NULL };
-
static gboolean Socks5UserPasswd(NetworkBuffer *NetBuf)
{
if (!NetBuf->userpasswd) {
t@@ -1132,75 +1066,6 @@ gboolean WriteDataToWire(NetworkBuffer *NetBuf)
}
}
-static void SendHttpRequest(HttpConnection *conn)
-{
- GString *text;
- char *userpasswd;
-
- conn->Tries++;
- conn->StatusCode = 0;
- conn->Status = HS_CONNECTING;
-
- text = g_string_new("");
-
- g_string_printf(text, "%s %s HTTP/1.0",
- conn->Method, conn->Query);
- QueueMessageForSend(&conn->NetBuf, text->str);
-
- g_string_printf(text, "Host: %s", conn->HostName);
- QueueMessageForSend(&conn->NetBuf, text->str);
-
- if (conn->Headers)
- QueueMessageForSend(&conn->NetBuf, conn->Headers);
-
- if (conn->user && conn->password) {
- userpasswd = g_strdup_printf("%s:%s", conn->user, conn->password);
- g_string_assign(text, "Authorization: Basic ");
- AddB64Enc(text, userpasswd);
- g_free(userpasswd);
- QueueMessageForSend(&conn->NetBuf, text->str);
- }
- if (conn->proxyuser && conn->proxypassword) {
- userpasswd =
- g_strdup_printf("%s:%s", conn->proxyuser, conn->proxypassword);
- g_string_assign(text, "Proxy-Authenticate: Basic ");
- AddB64Enc(text, userpasswd);
- g_free(userpasswd);
- QueueMessageForSend(&conn->NetBuf, text->str);
- }
-
- g_string_printf(text, "User-Agent: dopewars/%s", VERSION);
- QueueMessageForSend(&conn->NetBuf, text->str);
-
- /* Insert a blank line between headers and body */
- QueueMessageForSend(&conn->NetBuf, "");
-
- if (conn->Body)
- QueueMessageForSend(&conn->NetBuf, conn->Body);
-
- g_string_free(text, TRUE);
-}
-
-static gboolean StartHttpConnect(HttpConnection *conn)
-{
- gchar *ConnectHost;
- unsigned ConnectPort;
-
- if (conn->Proxy) {
- ConnectHost = conn->Proxy;
- ConnectPort = conn->ProxyPort;
- } else {
- ConnectHost = conn->HostName;
- ConnectPort = conn->Port;
- }
-
- if (!StartNetworkBufferConnect(&conn->NetBuf, conn->bindaddr, ConnectHost,
- ConnectPort)) {
- return FALSE;
- }
- return TRUE;
-}
-
static size_t MetaConnWriteFunc(void *contents, size_t size, size_t nmemb,
void *userp)
{
t@@ -1472,302 +1337,6 @@ void SetCurlCallback(CurlConnection *conn, GSourceFunc timer_cb,
curl_multi_setopt(conn->multi, CURLMOPT_SOCKETDATA, conn);
}
-gboolean OpenHttpConnection(HttpConnection **connpt, gchar *HostName,
- unsigned Port, gchar *Proxy,
- unsigned ProxyPort, const gchar *bindaddr,
- SocksServer *socks, gchar *Method,
- gchar *Query, gchar *Headers, gchar *Body)
-{
- HttpConnection *conn;
-
- g_assert(HostName && Method && Query && connpt);
-
- conn = g_new0(HttpConnection, 1);
-
- InitNetworkBuffer(&conn->NetBuf, '\n', '\r', socks);
- conn->HostName = g_strdup(HostName);
- if (Proxy && Proxy[0])
- conn->Proxy = g_strdup(Proxy);
- conn->bindaddr = g_strdup(bindaddr);
- conn->Method = g_strdup(Method);
- conn->Query = g_strdup(Query);
- if (Headers && Headers[0])
- conn->Headers = g_strdup(Headers);
- if (Body && Body[0])
- conn->Body = g_strdup(Body);
- conn->Port = Port;
- conn->ProxyPort = ProxyPort;
- *connpt = conn;
-
- if (StartHttpConnect(conn)) {
- SendHttpRequest(conn);
- return TRUE;
- } else {
- return FALSE;
- }
-}
-
-void CloseHttpConnection(HttpConnection *conn)
-{
- ShutdownNetworkBuffer(&conn->NetBuf);
- g_free(conn->HostName);
- g_free(conn->Proxy);
- g_free(conn->bindaddr);
- g_free(conn->Method);
- g_free(conn->Query);
- g_free(conn->Headers);
- g_free(conn->Body);
- g_free(conn->RedirHost);
- g_free(conn->RedirQuery);
- g_free(conn->user);
- g_free(conn->password);
- g_free(conn->proxyuser);
- g_free(conn->proxypassword);
- g_free(conn);
-}
-
-void SetHttpAuthentication(HttpConnection *conn, gboolean proxy,
- gchar *user, gchar *password)
-{
- gchar **ptuser, **ptpassword;
-
- g_assert(conn);
- if (proxy) {
- ptuser = &conn->proxyuser;
- ptpassword = &conn->proxypassword;
- } else {
- ptuser = &conn->user;
- ptpassword = &conn->password;
- }
- g_free(*ptuser);
- g_free(*ptpassword);
- if (user && password && user[0] && password[0]) {
- *ptuser = g_strdup(user);
- *ptpassword = g_strdup(password);
- } else {
- *ptuser = *ptpassword = NULL;
- }
- conn->waitinput = FALSE;
- if (conn->Status == HS_WAITCOMPLETE) {
- NetBufCallBack(&conn->NetBuf, TRUE);
- }
-}
-
-void SetHttpAuthFunc(HttpConnection *conn, HCAuthFunc authfunc,
- gpointer data)
-{
- g_assert(conn && authfunc);
- conn->authfunc = authfunc;
- conn->authdata = data;
-}
-
-static gboolean ParseHtmlLocation(gchar *uri, gchar **host, unsigned *port,
- gchar **query)
-{
- gchar *uris, *colon, *slash;
-
- uris = g_strstrip(uri);
- if (!uris || strlen(uris) < 7 || g_ascii_strncasecmp(uris, "http://", 7) != 0)
- return FALSE;
-
- uris += 7; /* skip to hostname */
-
- /* ':' denotes the port to connect to */
- colon = strchr(uris, ':');
- if (colon && colon == uris)
- return FALSE; /* No hostname */
-
- /* '/' denotes the start of the path of the HTML file */
- slash = strchr(uris, '/');
- if (slash && slash == uris)
- return FALSE; /* No hostname */
-
- if (colon && (!slash || slash > colon)) {
- if (slash)
- *slash = '\0';
- *port = atoi(colon + 1);
- if (slash)
- *slash = '\\';
- if (*port == 0)
- return FALSE; /* Invalid port */
- *host = g_strndup(uris, colon - uris);
- } else {
- *port = 80;
- if (slash)
- *host = g_strndup(uris, slash - uris);
- else
- *host = g_strdup(uris);
- }
-
- if (slash) {
- *query = g_strdup(slash);
- } else {
- *query = g_strdup("/");
- }
- return TRUE;
-}
-
-static void StartHttpAuth(HttpConnection *conn, gboolean proxy,
- gchar *header, gboolean *doneOK)
-{
- gchar *realm, **split;
-
- if (!conn->authfunc)
- return;
-
- split = g_strsplit(header, " ", 2);
-
- if (split[0] && split[1] && g_ascii_strncasecmp(split[0], "Basic", 5) == 0 &&
- g_ascii_strncasecmp(split[1], "realm=", 6) == 0 && strlen(split[1]) > 6) {
- realm = &split[1][6];
- conn->waitinput = TRUE;
- (*conn->authfunc) (conn, proxy, realm, conn->authdata);
- } else {
- *doneOK = FALSE;
- SetError(&conn->NetBuf.error, ÐTTP, HEC_BADAUTH, g_strdup(header));
- }
-
- g_strfreev(split);
-}
-
-static void ParseHtmlHeader(gchar *line, HttpConnection *conn,
- gboolean *doneOK)
-{
- gchar **split, *host, *query;
- unsigned port;
-
- split = g_strsplit(line, " ", 2);
- if (split[0] && split[1]) {
- if (g_ascii_strncasecmp(split[0], "Location:", 9) == 0 &&
- (conn->StatusCode == HEC_MOVETEMP
- || conn->StatusCode == HEC_MOVEPERM)) {
- if (ParseHtmlLocation(split[1], &host, &port, &query)) {
- g_free(conn->RedirHost);
- g_free(conn->RedirQuery);
- conn->RedirHost = host;
- conn->RedirQuery = query;
- conn->RedirPort = port;
- } else {
- *doneOK = FALSE;
- SetError(&conn->NetBuf.error, ÐTTP, HEC_BADREDIR,
- g_strdup(line));
- }
- } else if (g_ascii_strncasecmp(split[0], "WWW-Authenticate:", 17) == 0 &&
- conn->StatusCode == HEC_AUTHREQ) {
- StartHttpAuth(conn, FALSE, split[1], doneOK);
- } else if (g_ascii_strncasecmp(split[0], "Proxy-Authenticate:", 19) == 0 &&
- conn->StatusCode == HEC_PROXYAUTH) {
- /* Proxy-Authenticate is, strictly speaking, an HTTP/1.1 thing, but
- * some HTTP/1.0 proxies seem to support it anyway */
- StartHttpAuth(conn, TRUE, split[1], doneOK);
- }
- }
- g_strfreev(split);
-}
-
-gchar *ReadHttpResponse(HttpConnection *conn, gboolean *doneOK)
-{
- gchar *msg, **split;
-
- msg = GetWaitingMessage(&conn->NetBuf);
- if (msg)
- switch (conn->Status) {
- case HS_CONNECTING: /* OK, we should have the HTTP status line */
- conn->Status = HS_READHEADERS;
- split = g_strsplit(msg, " ", 3);
- if (split[0] && split[1]) {
- conn->StatusCode = atoi(split[1]);
- } else {
- *doneOK = FALSE;
- SetError(&conn->NetBuf.error, ÐTTP, HEC_BADSTATUS,
- g_strdup(msg));
- }
- g_strfreev(split);
- break;
- case HS_READHEADERS:
- if (msg[0] == 0)
- conn->Status = HS_READSEPARATOR;
- else
- ParseHtmlHeader(msg, conn, doneOK);
- break;
- case HS_READSEPARATOR:
- conn->Status = HS_READBODY;
- break;
- case HS_READBODY: /* At present, we do nothing special
- * with the body */
- break;
- case HS_WAITCOMPLETE: /* Well, we shouldn't be here at all... */
- g_free(msg);
- msg = NULL;
- break;
- }
- return msg;
-}
-
-gboolean HandleHttpCompletion(HttpConnection *conn)
-{
- NBCallBack CallBack;
- gpointer CallBackData, userpasswddata;
- NBUserPasswd userpasswd;
- gboolean retry = FALSE;
- LastError **error;
-
- error = &conn->NetBuf.error;
-
- /* If we're still waiting for authentication etc., then signal that the
- * connection shouldn't be closed yet, and go into the "WAITCOMPLETE"
- * state */
- if (conn->waitinput) {
- conn->Status = HS_WAITCOMPLETE;
- return FALSE;
- }
-
- if (conn->Tries >= 5) {
- SetError(error, ÐTTP, HEC_TRIESEX, NULL);
- return TRUE;
- }
-
- if (conn->RedirHost) {
- g_free(conn->HostName);
- g_free(conn->Query);
- conn->HostName = conn->RedirHost;
- conn->Query = conn->RedirQuery;
- conn->Port = conn->RedirPort;
- conn->RedirHost = conn->RedirQuery = NULL;
- retry = TRUE;
- }
- if (conn->StatusCode == HEC_AUTHREQ && conn->user && conn->password) {
- retry = TRUE;
- }
- if (conn->StatusCode == HEC_PROXYAUTH && conn->proxyuser &&
- conn->proxypassword) {
- retry = TRUE;
- }
-
- if (retry) {
- CallBack = conn->NetBuf.CallBack;
- userpasswd = conn->NetBuf.userpasswd;
- userpasswddata = conn->NetBuf.userpasswddata;
- CallBackData = conn->NetBuf.CallBackData;
- ShutdownNetworkBuffer(&conn->NetBuf);
- if (StartHttpConnect(conn)) {
- SendHttpRequest(conn);
- SetNetworkBufferCallBack(&conn->NetBuf, CallBack, CallBackData);
- SetNetworkBufferUserPasswdFunc(&conn->NetBuf,
- userpasswd, userpasswddata);
- return FALSE;
- }
- } else if (conn->StatusCode >= 300) {
- SetError(error, ÐTTP, conn->StatusCode, NULL);
- }
- return TRUE;
-}
-
-gboolean IsHttpError(HttpConnection *conn)
-{
- return (conn->NetBuf.error != NULL);
-}
-
int CreateTCPSocket(LastError **error)
{
int fd;
(DIR) diff --git a/src/network.h b/src/network.h
t@@ -148,55 +148,6 @@ struct _NetworkBuffer {
LastError *error; /* Any error from the last operation */
};
-/* Keeps track of the progress of an HTTP connection */
-typedef enum {
- HS_CONNECTING, /* Waiting for connect() to complete */
- HS_READHEADERS, /* Reading HTTP headers */
- HS_READSEPARATOR, /* Reading the header/body separator line */
- HS_READBODY, /* Reading HTTP body */
- HS_WAITCOMPLETE /* Done reading, now waiting for
- * authentication etc. before closing
- * and/or retrying the connection */
-} HttpStatus;
-
-typedef struct _HttpConnection HttpConnection;
-
-typedef void (*HCAuthFunc) (HttpConnection *conn, gboolean proxyauth,
- gchar *realm, gpointer data);
-
-/* A structure used to keep track of an HTTP connection */
-struct _HttpConnection {
- gchar *HostName; /* The machine on which the desired page
- * resides */
- unsigned Port; /* The port */
- gchar *Proxy; /* If non-NULL, a web proxy to use */
- unsigned ProxyPort; /* The port to use for talking to
- * the proxy */
- char *bindaddr; /* local IP address to bind to */
- gchar *Method; /* e.g. GET, POST */
- gchar *Query; /* e.g. the path of the desired webpage */
- gchar *Headers; /* if non-NULL, e.g. Content-Type */
- gchar *Body; /* if non-NULL, data to send */
- gchar *RedirHost; /* if non-NULL, a hostname to redirect to */
- gchar *RedirQuery; /* if non-NULL, the path to redirect to */
- unsigned RedirPort; /* The port on the host to redirect to */
- HCAuthFunc authfunc; /* Callback function for authentication */
- gpointer authdata; /* Data to be passed to authfunc */
- gboolean waitinput; /* TRUE if we're waiting for auth etc. to
- * be supplied */
- gchar *user; /* The supplied username for HTTP auth */
- gchar *password; /* The supplied password for HTTP auth */
- gchar *proxyuser; /* The supplied username for HTTP
- * proxy auth */
- gchar *proxypassword; /* The supplied password for HTTP
- * proxy auth */
- NetworkBuffer NetBuf; /* The actual network connection itself */
- gint Tries; /* Number of requests actually sent so far */
- gint StatusCode; /* 0=no status yet, otherwise an HTTP
- * status code */
- HttpStatus Status;
-};
-
void InitNetworkBuffer(NetworkBuffer *NetBuf, char Terminator,
char StripChar, SocksServer *socks);
void SetNetworkBufferCallBack(NetworkBuffer *NetBuf, NBCallBack CallBack,
t@@ -251,20 +202,6 @@ char *CurlNextLine(CurlConnection *conn, char *ch);
void SetCurlCallback(CurlConnection *conn, GSourceFunc timer_cb,
GIOFunc socket_cb);
-gboolean OpenHttpConnection(HttpConnection **conn, gchar *HostName,
- unsigned Port, gchar *Proxy,
- unsigned ProxyPort, const gchar *bindaddr,
- SocksServer *socks, gchar *Method,
- gchar *Query, gchar *Headers, gchar *Body);
-void CloseHttpConnection(HttpConnection *conn);
-gchar *ReadHttpResponse(HttpConnection *conn, gboolean *doneOK);
-void SetHttpAuthentication(HttpConnection *conn, gboolean proxy,
- gchar *user, gchar *password);
-void SetHttpAuthFunc(HttpConnection *conn, HCAuthFunc authfunc,
- gpointer data);
-gboolean HandleHttpCompletion(HttpConnection *conn);
-gboolean IsHttpError(HttpConnection *conn);
-
int CreateTCPSocket(LastError **error);
gboolean BindTCPSocket(int sock, const gchar *addr, unsigned port,
LastError **error);