http_post.h
[codes=php]
#ifndef __HTTP_POST__
#define __HTTP_POST__
#define SERVER_ADDR "123.57.252.183"
#define SERVER_PORT 80
#define SERVER_URL "ai.egg.levoo.com"
#define SERVER_PATH "/Api/upload"
#define HTTP_HEAD "POST %s HTTP/1.1\r\n"\
"Host: %s\r\n"\
"User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:59.0) Gecko/20100101 Firefox/59.0\r\n"\
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n"\
"Accept-Language: en-US,en;q=0.5\r\n"\
"Accept-Encoding: gzip, deflate\r\n"\
"Content-Type: multipart/form-data; boundary=%s\r\n"\
"Content-Length: %ld\r\n"\
"Connection: close\r\n"\
"Upgrade-Insecure-Requests: 1\r\n"\
"DNT: 1\r\n\r\n"\
#define UPLOAD_REQUEST "--%s\r\n"\
"Content-Disposition: form-data; name=\"image\"; filename=\"%s\"\r\n"\
"Content-Type: image/jpeg\r\n\r\n"
unsigned long get_file_size(const char *path);
int http_post_upload_pic(const unsigned char *IP, const unsigned int port,char *URL, const char *filepath,
char *ack_json, int ack_len); //Post方式上传图片
#endif
[/codes]
[codes=php]
#cat snprint.c
#include <stdlib.h>
#include <sys/types.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/stat.h>
#include "http_post.h"
unsigned char http_boundary[64]={0};
unsigned char send_request[1024]={0};
unsigned char send_end[1024]={0};
int main(int argc, char *argv[])
{
long long int timestamp;
struct timeval tv;
timestamp = (long long int)tv.tv_sec * 1000 + tv.tv_usec;
snprintf(http_boundary,64,"---------------------------%lld",timestamp);
const char *filepath = argv[1];
unsigned long totalsize = 0;
unsigned long filesize = -1;
unsigned long request_len = snprintf(send_request,1024,UPLOAD_REQUEST,http_boundary,filepath); //请求信息
unsigned long end_len = snprintf(send_end,1024,"\r\n--%s--\r\n",http_boundary); //结束信息
struct stat statbuff;
if(stat(filepath, &statbuff) < 0){
return filesize;
}else{
filesize = statbuff.st_size;
}
printf("eggpic.jpeg's filesize %ld\n",filesize);
return 0;
}
[/codes]
#gdb a.out
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-94.el7
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /data/codesdev/http_post/a.out...done.
(gdb) set args eggpic.jpeg
(gdb) b 23
Breakpoint 1 at 0x400645: file snprint.c, line 23.
(gdb) r
Starting program: /data/codesdev/http_post/a.out eggpic.jpeg
Breakpoint 1, main (argc=2, argv=0x7fffffffe798) at snprint.c:23
23 unsigned long end_len = snprintf(send_end,1024,"\r\n--%s--\r\n",http_boundary); //结束信息
(gdb) p argv[1]
$1 = 0x7fffffffea21 "eggpic.jpeg"
(gdb) p filepath
$2 = 0x7fffffffea1b "eggpic.jpeg"
if(stat(filepath, &statbuff) < 0){
(gdb) p send_request
$4 = '-' <repeats 29 times>, "4197109\r\nContent-Disposition: form-data; name=\"image\"; filename=\"eggpic.jpeg\"\r\nContent-Type: image/jpeg\r\n\r\n", '\000' <repeats 887 times>
30 printf("eggpic.jpeg's filesize %ld\n",filesize);
(gdb) p filesize
$3 = 13473
(gdb) n
eggpic.jpeg's filesize 13473
31 return 0;
(gdb) n
snprintf在C语言里字符串上,Http里使用较多,
int snprintf(char *restrict buf, size_t n, const char * restrict format, ...);
函数说明:最多从源串中拷贝n-1个字符到目标串中,然后再在后面加一个0。所以如果目标串的大小为n
的话,将不会溢出。
函数返回值:若成功则返回存入数组的字符数,若编码出错则返回负值。
Result1(推荐的用法)
#include <stdio.h>
#include <stdlib.h>
int main()
{
char str[10];
snprintf(str,sizeof(str),"0123456789012345678");
printf("str = %s \n",str);
return 0;
}
root@darkstar:/home/zhangl/unixtest/chapter9# ./testsprintf
str = 012345678
Result2:(不推荐使用)
#include <stdio.h>
#include <stdlib.h>
int main()
{
char str[10];
snprintf(str,18,"0123456789012345678");
printf("str = %s \n",str);
return 0;
}
root@darkstar:/home/zhangl/unixtest/chapter9# ./testsprintf
str = 01234567890123456
snprintf函数返回值的测试:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char str[10];
int n=0;
n=snprintf(str,sizeof(str),"%s","abc");
printf("str = %s \n",str);
printf("n=%d\n",n);
return 0;
}
Result:
root@darkstar:/home/zhangl/test# ./testsnprintf
str = abc
n=3
[codes=php]
#ifndef __HTTP_POST__
#define __HTTP_POST__
#define SERVER_ADDR "123.57.252.183"
#define SERVER_PORT 80
#define SERVER_URL "ai.egg.levoo.com"
#define SERVER_PATH "/Api/upload"
#define HTTP_HEAD "POST %s HTTP/1.1\r\n"\
"Host: %s\r\n"\
"User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:59.0) Gecko/20100101 Firefox/59.0\r\n"\
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n"\
"Accept-Language: en-US,en;q=0.5\r\n"\
"Accept-Encoding: gzip, deflate\r\n"\
"Content-Type: multipart/form-data; boundary=%s\r\n"\
"Content-Length: %ld\r\n"\
"Connection: close\r\n"\
"Upgrade-Insecure-Requests: 1\r\n"\
"DNT: 1\r\n\r\n"\
#define UPLOAD_REQUEST "--%s\r\n"\
"Content-Disposition: form-data; name=\"image\"; filename=\"%s\"\r\n"\
"Content-Type: image/jpeg\r\n\r\n"
unsigned long get_file_size(const char *path);
int http_post_upload_pic(const unsigned char *IP, const unsigned int port,char *URL, const char *filepath,
char *ack_json, int ack_len); //Post方式上传图片
#endif
[/codes]
[codes=php]
#cat snprint.c
#include <stdlib.h>
#include <sys/types.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/stat.h>
#include "http_post.h"
unsigned char http_boundary[64]={0};
unsigned char send_request[1024]={0};
unsigned char send_end[1024]={0};
int main(int argc, char *argv[])
{
long long int timestamp;
struct timeval tv;
timestamp = (long long int)tv.tv_sec * 1000 + tv.tv_usec;
snprintf(http_boundary,64,"---------------------------%lld",timestamp);
const char *filepath = argv[1];
unsigned long totalsize = 0;
unsigned long filesize = -1;
unsigned long request_len = snprintf(send_request,1024,UPLOAD_REQUEST,http_boundary,filepath); //请求信息
unsigned long end_len = snprintf(send_end,1024,"\r\n--%s--\r\n",http_boundary); //结束信息
struct stat statbuff;
if(stat(filepath, &statbuff) < 0){
return filesize;
}else{
filesize = statbuff.st_size;
}
printf("eggpic.jpeg's filesize %ld\n",filesize);
return 0;
}
[/codes]
#gdb a.out
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-94.el7
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /data/codesdev/http_post/a.out...done.
(gdb) set args eggpic.jpeg
(gdb) b 23
Breakpoint 1 at 0x400645: file snprint.c, line 23.
(gdb) r
Starting program: /data/codesdev/http_post/a.out eggpic.jpeg
Breakpoint 1, main (argc=2, argv=0x7fffffffe798) at snprint.c:23
23 unsigned long end_len = snprintf(send_end,1024,"\r\n--%s--\r\n",http_boundary); //结束信息
(gdb) p argv[1]
$1 = 0x7fffffffea21 "eggpic.jpeg"
(gdb) p filepath
$2 = 0x7fffffffea1b "eggpic.jpeg"
if(stat(filepath, &statbuff) < 0){
(gdb) p send_request
$4 = '-' <repeats 29 times>, "4197109\r\nContent-Disposition: form-data; name=\"image\"; filename=\"eggpic.jpeg\"\r\nContent-Type: image/jpeg\r\n\r\n", '\000' <repeats 887 times>
30 printf("eggpic.jpeg's filesize %ld\n",filesize);
(gdb) p filesize
$3 = 13473
(gdb) n
eggpic.jpeg's filesize 13473
31 return 0;
(gdb) n
snprintf在C语言里字符串上,Http里使用较多,
int snprintf(char *restrict buf, size_t n, const char * restrict format, ...);
函数说明:最多从源串中拷贝n-1个字符到目标串中,然后再在后面加一个0。所以如果目标串的大小为n
的话,将不会溢出。
函数返回值:若成功则返回存入数组的字符数,若编码出错则返回负值。
Result1(推荐的用法)
#include <stdio.h>
#include <stdlib.h>
int main()
{
char str[10];
snprintf(str,sizeof(str),"0123456789012345678");
printf("str = %s \n",str);
return 0;
}
root@darkstar:/home/zhangl/unixtest/chapter9# ./testsprintf
str = 012345678
Result2:(不推荐使用)
#include <stdio.h>
#include <stdlib.h>
int main()
{
char str[10];
snprintf(str,18,"0123456789012345678");
printf("str = %s \n",str);
return 0;
}
root@darkstar:/home/zhangl/unixtest/chapter9# ./testsprintf
str = 01234567890123456
snprintf函数返回值的测试:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char str[10];
int n=0;
n=snprintf(str,sizeof(str),"%s","abc");
printf("str = %s \n",str);
printf("n=%d\n",n);
return 0;
}
Result:
root@darkstar:/home/zhangl/test# ./testsnprintf
str = abc
n=3
作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:http://jackxiang.com/post/700/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!
最后编辑: jackxiang 编辑于2019-9-6 11:26
评论列表