If an HTTP request is sent using the socket (and compiled into a stress test script that can be called by LR)

Source: Internet
Author: User
Tags sprintf time in milliseconds

#include <unistd.h>
#include <sys/types.h> * * Basic system data types */
#include <sys/socket.h> * * BASIC SOCKET Definitions */
#include <netinet/in.h>/* sockaddr_in{} and other Internet Defns */
#include <arpa/inet.h>/* INET (3) functions */
#include <netdb.h>/*gethostbyname function */
#include <sys/time.h>
#include <time.h>
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>


#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <openssl/err.h>
#define Opensslkey "/test.key"
#define PUBLICKEY "/test_pub.key"
#define BUFFSIZE 1024
#define MAXLINE 10240


Char *my_encrypt (char *str,char *path_key) {
Char *p_en;
RSA *p_rsa;
FILE *file;
int Flen,rsa_len;
if ((File=fopen (Path_key, "R")) ==null) {
Perror ("Open key file Error");
return NULL;
}
if ((P_rsa=pem_read_rsa_pubkey (file,null,null,null)) ==null) {
if ((P_rsa=pem_read_rsapublickey (file,null,null,null)) ==null) {This is the way it's going to be. Regardless of whether the public key is detached from the source file
ERR_PRINT_ERRORS_FP (stdout);
return NULL;
}
Flen=strlen (str);
Rsa_len=rsa_size (P_RSA);
p_en= (unsigned char *) malloc (rsa_len+1);
memset (p_en,0,rsa_len+1);
if (Rsa_public_encrypt (Rsa_len, (unsigned char *) str, (unsigned char*) p_en,p_rsa,rsa_no_padding) <0) {
return NULL;
}
Rsa_free (P_RSA);
fclose (file);
return p_en;
}
Char *my_decrypt (char *str,char *path_key) {
Char *p_de;
RSA *p_rsa;
FILE *file;
int Rsa_len;
if ((File=fopen (Path_key, "R")) ==null) {
Perror ("Open key file Error");
return NULL;
}
if ((P_rsa=pem_read_rsaprivatekey (file,null,null,null)) ==null) {
ERR_PRINT_ERRORS_FP (stdout);
return NULL;
}
Rsa_len=rsa_size (P_RSA);
p_de= (unsigned char *) malloc (rsa_len+1);
memset (p_de,0,rsa_len+1);
if (Rsa_private_decrypt (Rsa_len, (unsigned char *) str, (unsigned char*) p_de,p_rsa,rsa_no_padding) <0) {
return NULL;
}
Rsa_free (P_RSA);
fclose (file);
return p_de;
}
Evp_pkey * GetKey (char * key_path) {
X509 *cert = NULL;
BIO *in = NULL;
Evp_pkey *key;


in = Bio_new_file (Key_path, "R");
Cert = pem_read_bio_x509 (in, NULL, NULL, NULL);
if (cert! = NULL) {
Key = (Evp_pkey *) X509_get_pubkey (cert);
} else {
in = Bio_new_file (Key_path, "R");
Key = Pem_read_bio_pubkey (in, Null,null, NULL);
}
Bio_free (in);
if (cert) {
X509_free (CERT);
}
Return key;
}


Char *my_encrypt2 (unsigned char *str,int * plen) {
int Cryptedlen;
unsigned char *cryptedbuf;
int i=0;
Evp_pkey *key;
Key = GetKey (PublicKey);

*plen = Evp_pkey_size (key);
Cryptedbuf = malloc ((*plen) + 1);
Memset (cryptedbuf,0, (*plen) + 1);


if (Rsa_public_encrypt (strlen (str), (unsigned char *) str,cryptedbuf,key->pkey.rsa,rsa_pkcs1_padding) <0) {
return NULL;
}
Evp_pkey_free (key);
for (i=0;i<128;i++) {
printf ("%d", (unsigned char) cryptedbuf[i]);
}
printf ("\ n");
return cryptedbuf;
}



int Opensocket (char *ip,int port)
{
int CONNFD;
char * servinetaddr = IP;
int servport = port;
Char Databuffer[maxline];
struct sockaddr_in servaddr;
Setting the time-out period
struct Timeval timeout={30,0};//3s

CONNFD = socket (af_inet, sock_stream, 0);
if ((Connfd=socket (af_inet,sock_stream,0)) = =-1)
{
Perror ("socket");
return 0;
}


if (setsockopt (Connfd,sol_socket,so_sndtimeo, (const char*) &timeout,sizeof (timeout)) = =-1)
{
Perror ("setsockopt");
return 0;
}


Bzero (&servaddr, sizeof (SERVADDR));
servaddr.sin_family = af_inet;
Servaddr.sin_port = htons (Servport);
Inet_pton (Af_inet, servinetaddr, &servaddr.sin_addr);



if (Connect (CONNFD, (struct sockaddr *) &servaddr, sizeof (SERVADDR)) < 0) {
Perror ("Connect error");
return-1;
}


else{
return 1;
return CONNFD;
}
}


void closesocket (int connfd) {
Close (CONNFD);
}

int msgsend (int connfd,char *msg, int len) {
Char Databuffer[maxline];
Bzero (databuffer,maxline+1);
strncpy (Databuffer,msg,strlen (msg));
int errsend = Send (connfd,databuffer,len,0);
if ( -1 = = errsend)
{
return-1;
}else{
return 1;
}
}


Send data after RSA encryption
int rsa_msgsend (int connfd,char * source)
{
Char *ptr_en,*ptr_de;
Char send_msg[1024];
int plen=0;
Ptr_en=my_encrypt2 (Source,&plen);
Char head[150];
sprintf (Head, "Post/auth http/1.1\r\nhost:bjcm01.portal.freemse.360.cn\r\naccept-version:1\r\nappid:1\r\ Nuser-agent:mse 8.0.0; Android 5.0\r\ncontent-length:%d\r\n\r\n ", Plen);
printf ("Plen:%d \ n", Plen);
printf ("Source is:%s\n", start);
Ptr_de=my_decrypt (Ptr_en,opensslkey);
printf ("Jiemi data%s\n", ptr_de);
sprintf (send_msg, "%s%s", start,ptr_en);
printf ("Send_msg%s\n", start);
int h_send_result = Msgsend (Connfd,head, strlen (head));
printf ("Head:%s\n", head);
int b_send_result = Msgsend (connfd,ptr_en, Plen);
printf ("Ptr_en:%s\n", ptr_en);
if (H_send_result = =-1 | | b_send_result = =-1) {
return-1;
}
else{
return 1;
}
}


Compare the returned results
int cmprecvmsg (int connfd,char *msg,int len) {
Char Databuffer[maxline];
Bzero (databuffer,maxline+1);
int recv_bytes = recv (connfd,databuffer,maxline,0);
return databuffer;
if (Recv_bytes < 0)
{
Return-2;
}


if (memcmp (Databuffer,msg,len) ==0) {
return 1;
}else{
return-1;
}

}



int gettimeofday (struct timeval *tv, struct timezone *tz);
int HTTPSENDANDRCV (int sockfd,char *realhost) {
struct Timeval t_start,t_end;
Long cost_time = 0;
#define BUFSIZE 1024
int ret, i,j, H;
struct sockaddr_in servaddr;
Char str1[4096], str2[4096], buf[bufsize];
Socklen_t Len;
Fd_set T_set1;
struct Timeval timeout={5,0}; Select Waits 5 seconds, 5 Seconds to poll, 0 for non-blocking


Send data
memset (str1, 0, 4096);//Be sure to initialize the memory, because there are some dirty data in the string that was sent during the test

strcat (str1, "get/http/1.1\n");
strcat (str1, "host:test.com\r\n");
strcat (str1, "realhost:");
strcat (str1, realhost);
strcat (str1, "\ r \ n");
strcat (str1, "content-type:application/x-www-form-urlencoded");
strcat (str1, "\r\n\r\n");
printf ("%s\n", str1);

Gettimeofday (&t_start, NULL);

Long start = ((long) t_start.tv_sec) *1000+ (long) t_start.tv_usec/1000;//in order to get the time in milliseconds, only the seconds and subtleties are converted to milliseconds to calculate the
printf ("Start time:%ld ms\n", start);
ret = Write (Sockfd,str1,strlen (str1));


if (Ret < 0) {
printf ("Send failed! The error code is%d and the error message is '%s ' \ n ', errno, Strerror (errno));
Exit (0);
}else{
printf ("Message sent successfully, total%d bytes sent!") \ n ", ret);
}
while (1) {
Sleep (1);
Fd_zero (&AMP;T_SET1);
Fd_set (SOCKFD, &t_set1);
H= 0;
printf ("--------------->1");
H= Select (sockfd +1, &t_set1, NULL, NULL, &timeout);
printf ("--------------->2");

if (h = = 0) break; The next round of requests can be made if no data is returned within the time-out period that is considered HTTP response accepted
if (H < 0) {
Close (SOCKFD);
printf ("Select detects an exception when reading a data message, and the exception causes the thread to terminate!") \ n ");
return-1;
};

if (H > 0) {
memset (buf, 0, 4096);
I= Read (SOCKFD, buf, 4095);
if (i==0) {
Close (SOCKFD);
printf ("Read the data message when the remote is turned off, the thread terminates!") \ n ");
return-1;
}
printf ("recive");
printf ("%s\n", buf);
}
}

Get End time
Gettimeofday (&t_end, NULL);
Long end = ((long) t_end.tv_sec) *1000+ (long) t_end.tv_usec/1000;
printf ("End time:%ld ms\n", end);
Statistics the time difference of the webpage response (including, of course, a timeout)
Cost_time = End-start;
printf ("Cost Time:%ld ms\n", cost_time);

return 0;
}


For testing
int main ()
{
unsigned char *source= "{\" phone\ ": \" 10002415407\ ", \" token\ ": \" 47\ ", \" wid\ ": \" 1\ "}";
Char *source= "{\" phone\ ": \" 1212121212\ ", \" token\ ": \" 121212121\ ", \" wid\ ": \" wid\ "}";
Char *ptr_en,*ptr_de;
Char *ip= "127.0.0.1";
int port=80
int CONNFD;
Char buf[20];
Char buf1[5];
char * realhost= "m.so.com";
Char send_msg[1024];
Char databuffer[21700];
Int J;
int plen=0;
Ptr_en=my_encrypt2 (Source,&plen);
Char start[150];
sprintf (Start, "Post/auth Http/1.1\r\nhost:bjcm01.portal.freemse.360.cn\r\naccept-version:1\r\nuser-agent:mse 8.0.0; Android 5.0\r\ncontent-length:%d\r\n\r\n ", Plen);
printf ("Plen:%d \ n", Plen);
printf ("Source is:%s\n", start);
Ptr_de=my_decrypt (Ptr_en,opensslkey);
printf ("Jiemi data%s\n", ptr_de);
Connfd=opensocket (Ip,port);
sprintf (send_msg, "%s%s", start,ptr_en);
printf ("Send_msg%s\n", start);
Msgsend (Connfd,start, strlen (start));
Msgsend (Connfd,ptr_en, Plen);
Rsa_msgsend (Connfd,source);
Recv (connfd,databuffer,21700,0);
SSCANF (DataBuffer, "%*[^\"]%[^m] ", buf);
SSCANF (buf, "%*[^:]:%[^,]", BUF1);
printf ("Recv%s\n", DataBuffer);
printf ("%s\n", BUF1);
for (j=0;j<3;j++) msgrecv (connfd,realhost);
Closesocket (CONNFD);
return 0;
}

If an HTTP request is sent using the socket (and compiled into a stress test script that can be called by LR)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.