为了自己能够在学习和工作中能够快速定位,快速理解,而浅浅的总结的。
希望在帮助自己的同时也能够帮助到各位码友。
- sscanf(),
- perror(),
- sprintf()
1.sscanf()
int parseRequestLine(const char* line, int cfd)
{
// 解析请求行
char method[12] = { 0 };
char path[1024] = { 0 };
char version[16] = { 0 };
sscanf(line, "%[^ ] %[^ ] %[^\r\n]", method, path, version);
decodeMsg(path, path);
printf("method: %s, path: %s, version: %s\n", method, path, version);
----------------------------------------------------------------------------------------
2.perror
perror("recv");
-----------------------------------------------------------------------------------------
3.sprintf()
int sendHeadMsg(int cfd, int status, const char* descr, const char* type, int length)
{
printf("START Response status line, header and blank line... \n");
// 状态行
char buf[4096] = { 0 };
sprintf(buf, "http/1.1 %d %s\r\n", status, descr);
// 响应头和空行
sprintf(buf + strlen(buf), "content-type: %s\r\n", type);
sprintf(buf + strlen(buf), "content-length: %d\r\n\r\n", length);
//sprintf(buf + strlen(buf), "\r\n");
send(cfd, buf, strlen(buf), 0);
printf("END Response status line, header and blank line... \n");
return 0;
}
if (strcasecmp(method, "get") != 0)
{
return -1;
}
- memcpy(),
- memset(),
- strcmp(),
- strrchr(),
- strstr()
1,2.memcpy,memset
void* recvHttpRequset(void* arg)
{
struct FdInfo* info = (struct FdInfo*)arg;
printf("\nstart communication.....\n");
int len = 0, total = 0;
char tmp[1024];
char buf[4096];
while ((len = recv(info->fd, tmp, sizeof(tmp), 0)) > 0)
{
if (total + len < sizeof(buf))
{
memcpy(buf + total, tmp, len);
memset(tmp, 0, sizeof(tmp));
}
total += len;
}
----------------------------------------------------------------------------------
3.strcmp
// 处理客户端请求的静态资源(目录或文件)
char* file = NULL;
if (strcmp(path, "/") == 0)
{
file = "./"; //请求的是资源目录
}
-------------------------------------------------------------------------------------
4.strrchr
// a.jpg, a.mp4, a.html........
// 自右向左查找'.'字符, 如果不存在返回null
const char* dot = strrchr(name, '.');
5. strstr
// 判断数据是否被接受完毕
if (len == -1 && errno == EAGAIN) //数据接受完毕
{
// 解析请求行
char* pt = strstr(buf, "\r\n"); //在大的字符串中寻找子串
int reqLen = pt - buf;
buf[reqLen] = '\0';
parseRequestLine(buf, info->fd);
}
// 获取文件属性
struct stat st;
int ret = stat(file, &st);
if (ret == -1)
{
// 文件不存在 --回复404
sendHeadMsg(cfd, 404, "Not Found", getFileType(".html"), -1);
sendFile("404.html", cfd);
return 0;
}
// 判断文件类型
if (S_ISDIR(st.st_mode))
{
// 把这个目录中的内容发送给客户端
printf("The request is for a directory...\n");
sendHeadMsg(cfd, 200, "OK", getFileType(".html"), -1);
sendDir(file, cfd);
}
else
{
// 把文件的内容发送给客户端
printf("The request is for a file...\n");
sendHeadMsg(cfd, 200, "OK", getFileType(file), st.st_size);
sendFile(file, cfd);
}
- lseek()
- ,chdir(): //切换当前进程的工作目录,chdir(argv[2]);
off_t offset = 0;
int size = lseek(fd, 0, SEEK_END); //求文件大小
printf("size: %ld\n", size);
lseek(fd, 0, SEEK_SET); //把文件指针移动到文件头
printf("============================01\n");
while (offset < size)
{
printf("============================02\n");
int ret = sendfile(cfd, fd, &offset, size - offset);
printf("ret value: %d\n", ret);
if (ret == -1 && errno == EAGAIN)
{
printf("没有数据 no data......\n");
/*perror("sendfile");*/
}
}
1.atoi把字符串转化为整形 unsigned short port = atoi(argv[1]);
2. malloc():struct FdInfo* info = (struct FdInfo*)malloc(sizeof(struct FdInfo));
3.free()