1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
char * appliAC (char * request_path, char * request_host, char * user, char * password, char * chemin, char * titre, char * auteur, char * description, int pub, int conf, char * dest) {
char buffer[PACKET_SIZE+1];
char line[PACKET_SIZE+2];
char param[PACKET_SIZE];
char body[PACKET_SIZE];
char * fic;
long taillefic=0;
int rc;
int i = 0;
int isEntete = 1;
char *finEntete;
FILE upload;
char * Reponse=(char *)calloc(10, sizeof(char));
sprintf(buffer,"Vide\n");
//recuperation des donnees
if ( ( upload = fopen(chemin,"rb")) == NULL ) {
Reponse="03";
}
else{
//calcul de la taille du fichier
fseek(upload, 0, SEEK_END);
taillefic=ftell(upload);
printf("Longueur du fichier %s: %lu\n", chemin, taillefic);
fclose(upload);
fic=(char *)calloc(taillefic, sizeof(char));
upload = fopen(chemin,"rb");
fread(fic, sizeof(char), taillefic, upload);
fclose(upload);
//contruction du corps de la requete
sprintf(param, "-----6G+f\r\n"
"Content-Disposition: form-data;name=\"cPseudo\"\r\n"
"\r\n"
"%s\r\n"
"-----6G+f\r\n"
"Content-Disposition: form-data;name=\"cMotDePasse\"\r\n"
"\r\n"
"%s\r\n"
"-----6G+f\r\n"
"Content-Disposition: form-data;name=\"cTitre\"\r\n"
"\r\n"
"%s\r\n"
"-----6G+f\r\n"
"Content-Disposition: form-data;name=\"cAuteur\"\r\n"
"\r\n"
"%s\r\n"
"-----6G+f\r\n"
"Content-Disposition: form-data;name=\"cDescription\"\r\n"
"\r\n"
"%s\r\n"
"-----6G+f\r\n"
"Content-Disposition: form-data;name=\"cPublic\"\r\n"
"\r\n"
"%d\r\n"
"-----6G+f\r\n"
"Content-Disposition: form-data;name=\"cConfidentiel\"\r\n"
"\r\n"
"%d\r\n"
"-----6G+f\r\n"
"Content-Disposition: form-data;name=\"cDestinataire\"\r\n"
"\r\n"
"%s\r\n"
"-----6G+f\r\n"
"Content-Disposition: form-data; name=\"CheminDoc\"; filename=\"%s\"\r\n"
"Content-type:application/octet-stream\r\n"
"Content-length:%d\r\n"
"\r\n"
, user, password, titre, auteur, description, pub, conf, dest, chemin, taillefic);
sprintf(body, "\r\n"
"-----6G+f--\r\n");
//construction de la requete
sprintf(line,
"POST %s "
"HTTP/1.1\r\n"
"Host: %s\r\n"
"Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*\r\n"
"Accept-Encoding: gzip, deflate\r\n"
"Content-Type: multipart/form-data; boundary=---6G+f\r\n"
"Content-length:%d\r\n\r\n"
,request_path, request_host, taillefic+strlen(param)+15);
//envoi de la requete par morceaux
printf ("%s", line);
send(to_server_socket,line,strlen(line),0);//entete
printf ("%s", param);
send(to_server_socket,param,strlen(param),0);//corps
send(to_server_socket, fic, taillefic, 0);
printf ("%s", body);
send(to_server_socket,body,strlen(body)+1,0);
free(fic);
do {
rc = readn(to_server_socket,buffer,PACKET_SIZE);
if ( isEntete == 1 ) {
finEntete = (char *)strstr(buffer,"\r\n\r\n");
if ( finEntete != NULL ) {
*finEntete = 0x00;
strncpy(Reponse, finEntete+4, rc-strlen(buffer)-4);
isEntete = 0;
}
else{
strncat(Reponse, buffer, rc);
}
}
else{
strncat(Reponse, buffer, rc);
}
} while (rc==PACKET_SIZE);
}
return Reponse;
} |
Partager