📄 httpr.c
字号:
/* * build http request headers * * Authors: Gerhard Khueny <e9625442@student.tuwien.ac.at> * * TODO: * * Fixes: * * For license terms, see the file COPYING in the project directory. */#include <stdio.h>#include <sys/types.h>#include <stdlib.h>#include <unistd.h>#include <glib.h>#include <string.h>#include "httpr.h"#define NORMAL_URL_CHAR "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-."gchar *gsms_http_url_encode(gchar * str){ gchar *new_url; GString *url; url=g_string_new(""); while (*str) { if (index(NORMAL_URL_CHAR, *str)) g_string_append_c(url,*str); else { if (*str == ' ') g_string_append_c(url,'+'); else if (*str == '\n') g_string_append(url,"%0D%0A"); else g_string_sprintfa(url, "%%%2X", (guchar) * str); } str++; } new_url=url->str; g_string_free(url,FALSE); return new_url;}gint gsms_http_create_message(GSmsHttp * h){ GString *message; if (strcmp(h->method, "GET") == 0) { if (h->url == NULL) return GSMS_HTTP_NO_URL; message= g_string_new(""); g_string_sprintfa(message, "GET %s", h->url); if (h->body != NULL) g_string_sprintfa(message, "?%s", h->body); } else if (strcmp(h->method, "POST") == 0) { if (h->url == NULL) return GSMS_HTTP_NO_URL; if (h->ctype == NULL) return GSMS_HTTP_NO_CTYPE; if (h->body == NULL) return GSMS_HTTP_NO_BODY; message= g_string_new(""); g_string_sprintfa(message, "POST %s", h->url); } else return GSMS_HTTP_INVALID_METHOD; if (h->protokoll == NULL) g_string_sprintfa(message, " HTTP/1.0\r\n"); else g_string_sprintfa(message, " %s\r\n", h->protokoll); if (h->referer != NULL) g_string_sprintfa(message,"Referer: %s\r\n", h->referer); if (h->host != NULL) g_string_sprintfa(message, "Host: %s\r\n", h->host); if (h->connection != NULL) g_string_sprintfa(message,"Connection: %s\r\n", h->connection); if (h->proxy_connection != NULL) g_string_sprintfa(message,"Proxy-Connection: %s\r\n", h->proxy_connection); if (h->acceptlang != NULL) g_string_sprintfa(message, "Accept-Language: %s\r\n", h->acceptlang); if (h->acceptchar != NULL) g_string_sprintfa(message, "Accept-Charset: %s\r\n", h->acceptchar); if (h->accept != NULL) g_string_sprintfa(message, "Accept: %s\r\n", h->accept); if (h->useragent != NULL) g_string_sprintfa(message, "User-Agent: %s\r\n", h->useragent); if (h->cookie != NULL) g_string_sprintfa(message, "Cookie: %s\r\n", h->cookie); /* For POST method set Content */ if (strcmp(h->method, "POST") == 0) g_string_sprintfa(message, "Content-Type: %s\r\nContent-Length: %d\r\n\r\n%s\r\n", h->ctype, strlen(h->body), h->body); else g_string_append(message,"\r\n"); h->message = message->str; g_string_free(message,FALSE); return GSMS_HTTP_OK;}void gsms_http_clean_up(GSmsHttp *ht){ g_free(ht->method); g_free(ht->url); g_free(ht->protokoll); g_free(ht->host); g_free(ht->referer); g_free(ht->accept); g_free(ht->acceptlang); g_free(ht->acceptchar); g_free(ht->connection); g_free(ht->cookie); g_free(ht->useragent); g_free(ht->body); g_free(ht->message);}gchar *gsms_http_strerror(gint error){ static gchar *errors[] = { "Success", "Invalid method", "No url", "No host", "No referer", "No Content-Type", "No BODY"}; if (error >= 0 && error < GSMS_HTTP_N_ERRORS) return errors[error]; else return "Unknown error";}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -